无法打开流:HTTP请求失败!检测到HTTP / 1.1 508环路

时间:2015-12-26 22:28:58

标签: php android json web-services

我正在创建一个Php Web服务来从httpurlconnection读取OutputStream。我有一个Android应用程序,它创建网站的httpurlconnection并将一个json字符串写入OutputStream。现在我想用Webservice读取/获取json。但我总是得到这个错误:

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

我会提供有关我申请的所有信息。如果有人知道更好的发送和接收数据的方式,请随时欢迎!

我的android应用程序在有onclick事件时发送数据,然后使用AsyncTask。这是代码:

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;
        }
    }
}

我的网络服务应用包含以下代码:

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;

当我运行webserwice时,例如:

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

我收到以下错误:

{"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}

我不知道如何解决这个问题。我迷失了自己。

1 个答案:

答案 0 :(得分:0)

你的错误是自我解释的,你有一个循环,这是服务器上的问题,与客户端(android部分)无关。在您的PHP中,您正在请求脚本:

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

以递归方式。要让json从android使用发送到你的脚本:

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