In my case on side PHP i use
$_POST = json_decode(file_get_contents("php://input"));
And on Android side
StringEntity se = new StringEntity(jsonObject.toString(),
HTTP.UTF_8);
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
request.setEntity(se);
PHP Manual - Input/Output PHP Streams
Full source of function sending POST JSON data to server from Android
public static void restApiJsonPOSTRequest(
final DefaultHttpClient httpclient, final HttpPost request,
final JSONObject jsonObject, final Handler responder,
final int responseCode, final int packetSize) {
new Thread(new Runnable() {
@Override
public void run() {
try {
try {
StringEntity se = new StringEntity(jsonObject
.toString(), HTTP.UTF_8);
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
request.setEntity(se);
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataInputStream dis = new DataInputStream(entity
.getContent());
byte[] buffer = new byte[packetSize];// In bytes
int realyReaded;
double contentSize = entity.getContentLength();
double readed = 0L;
while ((realyReaded = dis.read(buffer)) > -1) {
baos.write(buffer, 0, realyReaded);
readed += realyReaded;
sendDownloadingMessage(responder,
(double) contentSize, readed);
}
sendCompleteMessage(responder,
new InternetResponse(baos, responseCode,
request.getURI().toString()));
} else {
sendErrorMessage(responseCode, responder,
new Exception("Null"), request.getURI()
.toString());
}
} catch (ClientProtocolException e) {
sendErrorMessage(responseCode, responder, e, request
.getURI().toString());
} catch (IOException e) {
sendErrorMessage(responseCode, responder, e, request
.getURI().toString());
} finally {
httpclient.getConnectionManager().shutdown();
}
} catch (NullPointerException ex) {
sendErrorMessage(responseCode, responder, ex, request
.getURI().toString());
}
}
}).start();
}
or different way
public static void postJSONObject(final String url,
final Handler responder, final int responseCode,
final int packetSize, final JSONObject jsonObject,
final URLConnection connection) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(connection);
sendConnectingMessage(responder);
PrintWriter writer = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
HttpURLConnection htt = (HttpURLConnection) connection;
htt.setRequestMethod("POST");
OutputStream output = connection.getOutputStream(); // exception
// throws
// here
writer = new PrintWriter(new OutputStreamWriter(output,
"UTF-8"), true); // true = autoFlush, important!
String strJson = jsonObject.toString();
output.write(strJson.getBytes("UTF-8"));
output.flush();
System.out.println(htt.getResponseCode());
// Read resposne
baos = new ByteArrayOutputStream();
DataInputStream dis = new DataInputStream(
connection.getInputStream());
byte[] buffer = new byte[packetSize];// In bytes
int realyReaded;
double contentSize = connection.getContentLength();
double readed = 0L;
while ((realyReaded = dis.read(buffer)) > -1) {
baos.write(buffer, 0, realyReaded);
readed += realyReaded;
sendDownloadingMessage(responder, (double) contentSize,
readed);
}
sendCompleteMessage(responder, new InternetResponse(baos,
responseCode, url));
} catch (Exception e) {
sendErrorMessage(responseCode, responder, e, url);
} finally {
if (writer != null) {
writer.close();
}
}
}
}).start();
}