We are trying to send a large JSON object (with 100+ key value pairs) from an android app to server using following code.
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
JSONObject dt = new JSONObject();
try {
dt.put("test", "test");
} catch (JSONException e) {
e.printStackTrace();
}
urlConnection.setDoOutput(true);
// is output buffer writter
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
//set headers and method
Writer writer = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8"));
writer.write(String.valueOf(dt));
// json data
writer.close();
and in server side PHP,
$data = json_decode(file_get_contents('php://input'));
echo (string) $data;
But it returns blank value, how can we pass the JSON object from android app to server?
EDIT: We able to get the raw data without decoding by file_get_contents('php://input')
as {"test1":1234567890,"test2":"test"}
but when using json_decode
it returns blank value. We also tried with returning data withUTF-8
encoded format which sends data as %7B%22test1%22%3A1234567890%2C%22test2%22%3A%22test%22%7D
in raw format but it also unable to decode. BTW, json_last_error()
returns 4.