I'm calling a PHP file with an Android application with the next code:

 URL url=new URL("***********/add.php");HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();OutputStream out=new BufferedOutputStream(urlConnection.getOutputStream());writeStream(out);} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally{}

writeStream function:

 private void writeStream(OutputStream out) throws IOException {String output="Hello world";out.write(output.getBytes());out.flush();}

Then I have my PHP file where I'm trying to add the passed data to my database.This is it:

$response=array();// check for required fieldsif (isset($_POST['name'])) {$name=$_POST['name'];// include db connect classrequire_once __DIR__ . '/db_connect.php';// connecting to db$db=new DB_CONNECT();// mysql inserting a new row$result=mysql_query("INSERT INTO txumino(Laputxa) VALUES('$name')");// check if row inserted or notif ($result) {// successfully inserted into database$response["success"]=1;$response["message"]="Product successfully created.";// echoing JSON responseecho json_encode($response);} else {// failed to insert row$response["success"]=0;$response["message"]="Oops! An error occurred.";// echoing JSON responseecho json_encode($response);}} else {// required field is missing$response["success"]=0;$response["message"]="Required field(s) is missing";// echoing JSON responseecho json_encode($response);}

What I don't get is how i give my data a name to get it with the index as I do in the lines I use this:

$_POST['name']

I have done it previously using an HTML form, but I don't know how it works in Java.

share|improve this question
  • 2
    2018 and people still using mysql_* library(which is deprecated for a long time now and people should AVOID it), what a sad thing... You mean you want your json response to be named as response? just make all the child, a child of $response["response"]– PrixFeb 14 at 21:20
  • No, sorry if it'n not well explained, I'm not English. What I want to know is how to give the data I'm passing from Java to PHP ("Hello World" in this case) to take it with the name index ($_POST["name"])– Mikel MolinuevoFeb 14 at 21:22
  • That makes more sense now, from your above code example it does not show where its being set, but its the post parameter. Some examples of it Java - sending HTTP parameters via POST method easily– PrixFeb 14 at 21:25
  • Yes, the correct answer would be the second one of that post, thanks– Mikel MolinuevoFeb 14 at 21:28
  • If you look at the code examples on the page I just sent you, you can see you can define the "KEY" aka "INDEX" for every parameter when sending it from the java code. Its a "key > value" association.– PrixFeb 14 at 21:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.