I am using this code to send this to my php file. The file looks what is coming like this.
file_put_contents(\'dump.txt\', \"POST: \\n\" . print_r($_POST, true)
the way I send JSON with my DefaultHttpClient
in my app work perfectly:
public static HttpUriRequest createPostForJSONObject(
JSONObject params, String url) {
HttpPost post = new HttpPost(url);
post.setEntity(createStringEntity(params));
return post;
}
private static HttpEntity createStringEntity(JSONObject params) {
StringEntity se = null;
try {
se = new StringEntity(params.toString(), "UTF-8");
se.setContentType("application/json; charset=UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Failed to create StringEntity", e);
exception = e;
}
return se;
}
in order to run this request in your code all you need to do is this:
public void postData(String url,JSONObject json) {
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost post = createPostForJSONObject(json, url);
HttpResponse response = httpClient.execute(post);
// DO YOUR THING
} catch Exception {
// HANDLE EXCEPTION
}
}