1

I have done raw rest get and post and also could do post in namevaluepair but no matter which tutorial i follow i just cant get json post done ...

Android Application:

ArrayList<String> stringData = new ArrayList<String>();
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost postMethod = new HttpPost("http://192.168.1.10/people.php");     

JSONObject holder = new JSONObject();
holder.put("name", "Foo");    
holder.put("occupation", "Bar");

StringEntity se = new StringEntity(holder.toString());
postMethod.setEntity(se);
postMethod.setHeader("Accept", "application/json");
postMethod.setHeader("Content-type", "application/json");

ResponseHandler <String> resonseHandler = new BasicResponseHandler();
String response=httpClient.execute(postMethod,resonseHandler); 
stringData.add(response);

return stringData;

PHP file:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

 var_dump($_POST);
}

the var_dump just gives an empty array and $_POST["name"] gives undefined index ... any help would be nice

0

3 Answers 3

2

Have you tried to ship the holder.toString() with BasicNameValuePair class?

Like

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("myjson", holder.toString());
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

then just in your php you get it like

$jsonstring = $_REQUEST['myjson'];
Sign up to request clarification or add additional context in comments.

2 Comments

Maybe you need to decode it in your php code. If decoding is needed use $jsonstring = urldecode($_REQUEST['myjson'];
This answer is useful for me :)
0

Two additional ways to read POST data:

  1. Read from php://input
  2. Use var $HTTP_POST_RAW_DATA.

Look at http://www.php.net/manual/en/reserved.variables.httprawpostdata.php

Comments

0

I think the problem might be the url you are using.

Post to 10.0.2.2 instead of the one you specified in your http post.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.