0

I'm making a Php Webservice to read the OutputStream from a httpurlconnection. I have a android app that creates the httpurlconnection to the website and writes a json string to the OutputStream. Now I want to read/get that json with the Webservice. But I always get this error:

failed to open stream: HTTP request failed! HTTP/1.1 508 Loop Detected

I will give all the information about my application. If someone knows a better way to send and receive the data, always welcome!

My android application sends the data when there is an onclick event and then uses a AsyncTask. here's the code:

public void clickbuttonRecieve(View v) {
    MyAsync performBackgroundTask = new MyAsync();
    performBackgroundTask.execute();
}



protected Void doInBackground(Void... tmps) {
        try {
            // instantiate the URL object with the target URL of the resource to
            // request
            URL url = new URL("http://bomatec.be/webservice2.php");

            // instantiate the HttpURLConnection with the URL object - A new
            // connection is opened every time by calling the openConnection
            // method of the protocol handler for this URL.
            // 1. This is the point where the connection is opened.
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            // set connection output to true
            connection.setDoOutput(true);
            // instead of a GET, we're going to send using method="POST"
            connection.setRequestMethod("POST");

            // instantiate OutputStreamWriter using the output stream, returned
            // from getOutputStream, that writes to this connection.
            // 2. This is the point where you'll know if the connection was
            // successfully established. If an I/O error occurs while creating
            // the output stream, you'll see an IOException.
            OutputStreamWriter writer = new OutputStreamWriter(
                    connection.getOutputStream());

            // write data to the connection. This is data that you are sending
            // to the server
            // 3. No. Sending the data is conducted here. We established the
            // connection with getOutputStream
            JSONObject data = new JSONObject();
            data.put("user", "12");
            data.put("lat", "50.8");
            data.put("lng", "4.3");
            String json = data.toString();
            writer.write("json=" + json);

            // Closes this output stream and releases any system resources
            // associated with this stream. At this point, we've sent all the
            // data. Only the outputStream is closed at this point, not the
            // actual connection
            writer.close();
            // if there is a response code AND that response code is 200 OK, do
            // stuff in the first if block
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                // OK

                // otherwise, if any other status code is returned, or no status
                // code is returned, do stuff in the else block
            } else {
                // Server returned HTTP error code.
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }
}

My webservice app contains the following code :

header('Content-Type: application/json');
$json = file_get_contents('http://bomatec.be/webservice2.php?');
$obj = json_decode($json);
echo json_encode(array('json'=>$obj));
echo $json;

When I run the webserwice, example :

http://bomatec.be/webservice2.php?json={%22user_id%22:31,%22lat%22:50.78,%22lng%22:2}

I get following error :

{"json":null}{"json":null}{"json":null}{"json":null}{"json":null}{"json":null}{"json":null}{"json":null}{"json":null}{"json":null}{"json":null}{"json":null}{"json":null}{"json":null}{"json":null}{"json":null}{"json":null}{"json":null}{"json":null}<br />
<b>Warning</b>:  file_get_contents(http://bomatec.be/webservice2.php?): failed to open stream: HTTP request failed! HTTP/1.1 508 Loop Detected
 in <b>/home/bomate1q/public_html/webservice2.php</b> on line <b>15</b><br />
{"json":null}

I don't know how to solve this. I lost myself in it.

Steve Ritz
  • 2,017
  • 4
  • 12
  • 12

1 Answers1

0

Your error is self explanatory, you have a loop and this is a problem on server and has nothing to do with client (android part). In your php you are requesting script:

$json = file_get_contents('http://bomatec.be/webservice2.php?');

in recursive manner. To get json send to your script from android use:

$json = file_get_contents('php://input');
marcinj
  • 48,511
  • 9
  • 79
  • 100
  • What can I do about that? – Steve Ritz Dec 26 '15 at 22:46
  • why do you need this `file_get_contents('http://bomatec.be/webservice2.php?');` ? – marcinj Dec 26 '15 at 22:48
  • I was following this tutorial : http://www.codeproject.com/Articles/267023/Send-and-receive-json-between-android-and-php I want the json string that is send with android to "http://bomatec.be/webservice2.php" – Steve Ritz Dec 26 '15 at 22:51
  • change this line to : `$json = file_get_contents('php://input');` – marcinj Dec 26 '15 at 22:54
  • Then my $json is null. Problem of my client? – Steve Ritz Dec 26 '15 at 22:58
  • I test is by going to "http://bomatec.be/webservice2.php?json={%22user_id%22:31,%22lat%22:50.78,%22lng%22:2}" is that a good way to test it? Or am I sending no data like this? – Steve Ritz Dec 26 '15 at 23:00
  • No - if you use webbrowser then you use get - whilie you should use POST. I tested it with curl : `curl -i -X POST -d '{"test1":"test2"}' http://bomatec.be/webservice2.php` - and it looks fine, – marcinj Dec 26 '15 at 23:06
  • @SteveRitz your android code looks fine, you might want to check this SO: http://stackoverflow.com/questions/32175002/file-get-contentsphp-input-showing-null-when-var-dump – marcinj Dec 26 '15 at 23:12