0

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.

mpsbhat
  • 2,733
  • 12
  • 49
  • 105
  • What version of PHP are you using? See http://php.net/manual/de/wrappers.php.php for limitations before v5.6. Why don't you use the $_POST superglobal variable to receive your data? Also ensure that post_max_size is high enough, see: http://php.net/manual/de/ini.core.php#ini.post-max-size – user1915746 Jun 26 '17 at 11:25
  • We are using PHP5.4.45, we did tried `$_POST` also which returned NULL. – mpsbhat Jun 26 '17 at 11:33
  • you are decoding to a php `stdClass` , which cant be cast to string. Your php is probably failing on the `echo` line. – YvesLeBorg Jun 26 '17 at 11:35
  • Also tried `$data = $_POST['test']; echo $data;` but returns NULL. – mpsbhat Jun 26 '17 at 11:41
  • Try do do it step by step. Always use var_dump for debugging variables. What do you get by var_dump-ing the whole $_POST variable before json decode? json_decode can result in "null" if there are some BOM characters. See also https://stackoverflow.com/questions/2410342/php-json-decode-returns-null-with-valid-json – user1915746 Jun 26 '17 at 11:51
  • @user1915746, see the edit in question. – mpsbhat Jul 01 '17 at 12:03
  • 1
    Error 4 means JSON_ERROR_SYNTAX so something with your json object seems to be wrong. It works for me if I send exactly your json object with POST to a php file and then read it like that. Your second format is url encoded, so you have to decode it first. `$input = file_get_contents('php://input'); $obj = json_decode($input); var_dump($obj); $encoded = '%7B%22test1%22%3A1234567890%2C%22test2%22%3A%22test%22%7D'; $decoded = urldecode($encoded); var_dump(json_decode($decoded));` – user1915746 Jul 01 '17 at 14:26

0 Answers0