0

I´m currently having problems with exchanging JSON Objects between my Android Appliction and my Server Backend. For whatever reason

$json = file_get_contents('php://input');

doesn´t work. $json always is null. The App sends the JSON Object with:

        url = new URL("www.example.com/app/request.php");
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setDoOutput(true);
        urlConnection.setChunkedStreamingMode(0);
        OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
        OutputStreamWriter wr = new OutputStreamWriter(out);
        wr.write(request.toString());
        Log.i(TAG, "OUT: " + request.toString());
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line;
        StringBuilder stringbuilder = new StringBuilder();
        while ((line = br.readLine()) != null){
            stringbuilder.append(line);
            Log.i(TAG, "Stringline: " + line);

        }
        in.close();
        Log.i(TAG, "Server responds: " + stringbuilder.toString());

The php-Script simply runs:

        <?php
        echo file_get_contents('php://input');
        ?>

So it simply returns the JSONObject back to the sender. Anyway the Logs says:

      OUT: {"firstname":"Foo"}
      Server responds:

then breaks down on JSONObjection cause of course null can´t be parsed to JSONOBject. Appache error_log doesn´t display a error, otherwise the Android Log would also crash into a FileNotFoundException. access_ssllog logs POST on correct file by correct device.

What did I do wrong? I rembember that php://input doesn´t work under some Server Conditions... What would be the alternatives?

Any help is appreciated

  • 1
    Looks like you're missing the header configuration stuff, i.e. `conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");`. Take a look at the accepted answer in the linked question. – Daniel Nugent Feb 02 '17 at 18:29
  • 1
    Thanks, used his code which solved the Problem – Tim Vahlbrock Feb 03 '17 at 11:08

0 Answers0