Reputation: 505
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
Upvotes: 1
Views: 3370
Reputation: 947
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.
Upvotes: 0
Reputation: 50538
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'];
Upvotes: 2
Reputation: 7504
Two additional ways to read POST data:
Look at http://www.php.net/manual/en/reserved.variables.httprawpostdata.php
Upvotes: 0