maybe its duplicate but i cant find it.
something = new ArrayList<>();
something.add(new Object("Hello"));
something.add(new object("World"));
something.add(new Object("!"));
for(blablabla){
System.out.print(something.get(i).getTextFromConstructor());
}
this will print "Hello World!"
in php i dont know solution to pass whole objects into array to call their methods from loop or just by something[0]->method();
As i know this in php cannnot be done, but maybe i am wrong :-)
Thank You
$arr = array();
$arr[] = new MyUSerDefinedObject("Hello");
//...
echo $arr[0]->methd();
//or
foreach ($arr as $val) {
echo $val->methd();
}
Use this example to set the bridge b/w php and java so that you can pass values to them
<?php
$date = new Java("java.util.Date", 70, 9, 4);
var_dump($date->toString());
$map = new Java("java.util.HashMap");
$map->put("title", "Java Bridge!");
$map->put("when", $date);
echo $map->get("when")->toString()."\n";
echo $map->get("title")."\n";
$array = array(1,2,3,4,5);
$map->put("stuff", $array);
var_dump($map->get("stuff"))."\n";
$system = new JavaClass("java.lang.System");
echo "OS: ".$system->getProperty("os.name")."\n";
$math = new JavaClass("java.lang.Math");
echo "PI: ".$math->PI."\n";
?>
Related
I am sending JSON Objects which contain converted ArrayLists. For the most part everything works fine except on the PHP page.
If I use:
if($_POST)
{
echo "Something was sent";
$obj = array();
$JSON_Entry = $_POST["Entry"];
$body = json_decode($JSON_Entry, true);
foreach ($body as $key => $value)
{
echo $value;
}
I get the response in android emulator logs;
Something was sent[SalesMade [id=0, product_description=Beer, qty=2, unit=3, total=6.0]]
But when I try to separate the array using:
foreach($value as $column => $row)
{
echo $row;
}
I get an Invalid argument supplied for foreach() error. Is it because I converted the ArrayList to a JSON object before posting?
First you have to json_decode() $value, then you can foreach() through it.
I want to create a Java Application, which creates an user account.
After this, the user should be able to login on my website.
I need a Java method to generate a hash, which is supported by PHP.
In PHP I always used this function to generate a hash:
public function hashPassword($password){
$options = [
'cost' => 11,
];
$password."<br>";
$hash = password_hash($password, PASSWORD_BCRYPT, $options)."<br>";
return $hash;
}
How can I do this in Java, when ...
I use this in PHP, to validate the password
public function checkPassword($password, $hash){
if (password_verify($passwordFromPost, $hash)) {
echo 'Password is valid!';
} else {
echo 'Invalid password.';
}
}
PS: to generate the $hash, I use the first function.
If anything isn't correct, please correct my code, because I'm new in Java
I want to send a String[] by an HTTP request and get the values in PHP with the $_GET method.
The total number of values in the String[] is variable.
I have tried so far:
List<NameValuePair> params = new ArrayList<NameValuePair>();
String[] dropdowns = {"1st item","2nd item","3rd item","4th item"};
for (int i = 0; i < dropdowns.length; i++) {
params.add(new BasicNameValuePair("pid", dropdowns[i]));
}
In PHP I want to get all values and query based on them.
$pid = $_GET['pid'];
And use them like:
$result = mysql_query("SELECT *FROM Apps WHERE pid[0] = $pid" AND pid[1] = $pid"
AND ...);
But I know this way is wrong.
How can I do that?
This
$result = mysql_query("SELECT *FROM Apps WHERE pid[0] = $pid" AND pid[1] = $pid" AND ...);
Is very wrong and unsafe. (Columns wrong syntax, SQL injection, wrong quotation, wrong SQL syntax,...)
Must be something like
$result = mysql_query("
SELECT * FROM Apps WHERE pid
IN(" . implode(',', mysql_real_escape_string($pid)) . ")
");
You can create a serialized reprezentation of the values you want to send in the url. It has limitations such as the max length of the url.
'http://domain.com/data_handler.php?data=' . urlencode(serialize($array1));
Getting back your array:
$array1 = unserialize($_GET['data']);
Its even better to create a post request and use this syntax:
pid[]=1
pid[]=2
http://www.php.net/manual/en/faq.html.php
You cannot send an array through HTTP request UNLESS you have an array of inputs such as:
<input type='text' name='manyOfThese[]' />
To send an array you have two options. One is to use serialize() and unserialize() to turn your array into a string. And the other is to use session variables:
$_SESSION['pid'] = $pid;
Then on the next script
$pid = $_SESSION['pid'];
unset($_SESSION['pid']);
foreach($pid as $element){
echo $element //or do whatever you need to do to that variable
}
Also at the beginning of your scripts you will want to include:
session_start();
And then when your php application is exited (upon logoff for example):
session_destroy();
There are two parts to this and both involve loops. First, when you are sending the data, put the brackets in the name to send it as an array:
for (int i = 0; i < dropdowns.length; i++) {
params.add(new BasicNameValuePair("pid[]", dropdowns[i]));
}
Second, on the php end this array is stored in $_GET['pid'] or $_POST['pid'] depending on how you sent it, so you would loop through the array and add the items to your sql query. Just make a separate variable to store the sql statement so you can add to it:
$x = 0;
foreach($_GET['pid'] as $value) {
$yourSQLString .= " AND pid[". $x ."] = '" . $value . "'";
$x++;
}
And obviously you should do something else with the actual value to avoid sql injections.
here is my php code
$titikPetaInti = array();
while($row = mysql_fetch_assoc($hasil2))
{
$titikPetaInti[] = $row['koordinat'];
}
$data = "{titikPeta:".json_encode($titikPetaInti)."}";
echo $data;
?>
then here is my android code
xResultTitikPeta is result request to php
jObject = new JSONObject(xResultTitikPeta);
JSONArray myArray1 = (JSONArray) jObject.getJSONArray("titikPeta");
String[]titikPeta = new String[myArray1.length()];
for(int a = 0; a < myArray1.length(); a++)
{
titikPeta[a] = myArray1.getJSONObject(a).toString();
}
teks1 = (TextView) findViewById(R.id.textView1);
teks1.setText(Arrays.toString(titikPeta));
it displaying null at emulator like no value
--EDIT--
i think there something mistake in parsing code, cus when i display the xResultTitikPeta in android, it give me string result
here is result of xResultTitikPeta
{titikPeta:["-8.705378,115.225189","-8.56056700000000,115.42395100000","-8.57659700000000,115.40065300000","-8.55596300000000,115.41085700000","-8.51855200000000,115.491908000000","-8.54743200000000,115.41036800000","-8.56551100000000,115.45173900000","-8.44321000000000,115.616019000000"]}
this is malformed JSON! no double quotes on key.
$data = "{titikPeta:".json_encode($titikPetaInti)."}";
instead do:
$data = '{"titikPeta":'.json_encode($titikPetaInti).'}';
EDITED:
Ok, remove that hand made approach:
$data = json_encode(array("titikPeta"=>$titikPetaInti));
OK, I've found your bug! As well as fixing the $data = json_encode(array("titikPeta" => $titikPetaInti)); issue, the problem is here:
titikPeta[a] = myArray1.getJSONObject(a).toString();
The elements of myArray1 are actually of type string and cause an exception to be thrown, so you need instead:
titikPeta[a] = myArray1.getString(a);
This produces the output of:
[-8.705378,115.225189, -8.56056700000000,115.42395100000, -8.57659700000000,115.40065300000, -8.55596300000000,115.41085700000, -8.51855200000000,115.491908000000, -8.54743200000000,115.41036800000, -8.56551100000000,115.45173900000, -8.44321000000000,115.616019000000]
As each element in your array is of the form "-8.705378,115.225189", the JSON parser assumes they are strings. If you change the elements to "-8.705378","115.225189" you can also use:
titikPeta[a] = Double.toString(myArray1.getDouble(a));
However, the first version will work too.
Note: my personal preference is that I would declare each array element as:
{"x":-8.705378,"y":115.225189}
try
$data = "{\"titikPeta\":".json_encode($titikPetaInti)."}";
I have a list of web addresses such as listed below in my DB.
I need to get the domain name from each address in the list.
http://en.wordpress.com/tag/1000-things-we-hate/
http://en.wordpress.com/tag/1019/
http://en.wordpress.com/tag/1030-am/
http://www.yahoo.com/index.html
http://www.msn.com/index.html
Here's a way to do it in Java:
String input = "http://en.wordpress.com/tag/1000-things-we-hate/";
// Assuming that all urls start with "http://"
int finish = input.indexOf("/", 7);
if(finish == -1)
{
finish = input.length();
}
System.out.println(input.substring(7, finish));
Prints en.wordpress.com (I assume that is what you want?)
<?php
$url = "http://en.wordpress.com/tag/1000-things-we-hate/";
$bits = explode("/",$url);
$nextBits = explode(".",$bits[1]);
$count = count($nextBits);
$domain = $nextBits[$count-1].".".$nextBits[$count];
echo $domain;
?>
<?php
echo parse_url($url, PHP_URL_HOST);
That would return "en.wordpress.com". If you don't want subdomains (i.e. only "wordpress.com), then things are getting complicated. You would need something like http://www.dkim-reputation.org/regdom-libs/
Use the parse_url in PHP.