Android HttpURLConnection - 意外状态行:

时间:2015-11-25 16:34:20

标签: java android httprequest httpurlconnection

我一直在努力解决问题一段时间,这让我疯了。

我使用HttpClient的弃用方法有一些(工作)代码。此代码只是在我的Web服务器上调用PHP脚本,并读取它返回的字符串。我决定将它更新为HttpURLConnection,因此代码可以“与时俱进”。

以下是工作中已弃用的代码:

URL url = new URL(link);
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();

String nullFragment = null;
URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), nullFragment);
request.setURI(uri);

HttpResponse response = client.execute(request);
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

String inputLine;
while ((inputLine = in.readLine()) != null){
    this.finalString = inputLine;
}

简单的东西,工作正常。

此代码用于调用不断返回简单字符串的PHP脚本。这个字符串总是简单而简短,如“OK_1”,“ERR_TYPE_1”,“OK_2”等等。

所以我尝试使用HttpURLConnection将这个非常简单的代码更新为一个非常简单的方法。 我尝试了很多变化,但要点是:

StringBuilder sb = new StringBuilder();

BufferedInputStream bis = null;
URL url = null;
try {
    url = new URL(urlString);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();

    con.setConnectTimeout(10000);
    con.setReadTimeout( 10000 );

    bis = new java.io.BufferedInputStream(con.getInputStream());
    BufferedReader reader = new BufferedReader(new InputStreamReader(bis));
    String line = null;

    while ((line = reader.readLine()) != null)
        sb.append(line);

    bis.close();

} catch (MalformedURLException e) {
    e.printStackTrace();
}catch (IOException e) {
    e.printStackTrace();
}

无论我运行什么变体,当我尝试调用getInputStream时,我总是意外的状态行:

The exception is:
java.net.ProtocolException: Unexpected status line: 

请注意,它显示意外状态行值为“空”。

我意识到这是一个可能与服务器端相关的问题,但我的php脚本将始终只返回一个像我提到的单个字符串。我能做些什么吗?我真的很感激任何提示,因为我即将放弃并回到HttpClient,尽管这是一种弃用的方法,但它仍然运行良好。

非常感谢任何帮助。

编辑: Logcat异常打印:

  

11-26 08:57:02.602 1109-1954 / app.mission.manager W / System.err:   java.net.ProtocolException:意外的状态行:11-26   08:57:02.603 1109-1954 / app.mission.manager W / System.err:at   com.android.okhttp.internal.http.StatusLine。(StatusLine.java:38)   11-26 08:57:02.603 1109-1954 / app.mission.manager W / System.err:at   com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:180)   11-26 08:57:02.603 1109-1954 / app.mission.manager W / System.err:at   com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:101)   11-26 08:57:02.603 1109-1954 / app.mission.manager W / System.err:at   com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:636)   11-26 08:57:02.603 1109-1954 / app.mission.manager W / System.err:at   com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:397)   11-26 08:57:02.603 1109-1954 / app.mission.manager W / System.err:at   com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:341)   11-26 08:57:02.603 1109-1954 / app.mission.manager W / System.err:at   com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:509)   11-26 08:57:02.603 1109-1954 / app.mission.manager W / System.err:at   app.mission.manager.Login $ LoginValidator.doInBackground(Login.java:82)   11-26 08:57:02.603 1109-1954 / app.mission.manager W / System.err:at   app.mission.manager.Login $ LoginValidator.doInBackground(Login.java:39)   11-26 08:57:02.603 1109-1954 / app.mission.manager W / System.err:at   android.os.AsyncTask $ 2.call(AsyncTask.java:292)11-26 08:57:02.603   1109-1954 / app.mission.manager W / System.err:at   java.util.concurrent.FutureTask.run(FutureTask.java:237)11-26   08:57:02.603 1109-1954 / app.mission.manager W / System.err:at   android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)11-26   08:57:02.603 1109-1954 / app.mission.manager W / System.err:at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)   11-26 08:57:02.603 1109-1954 / app.mission.manager W / System.err:at   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587)   11-26 08:57:02.603 1109-1954 / app.mission.manager W / System.err:at   java.lang.Thread.run(Thread.java:818)

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此代码,如果它仍然无效,我确定这是服务器端问题。

try {
    URL url = new URL(urlString);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setConnectTimeout(10000);
    connection.setReadTimeout(10000); 

    int responseCode = connection.getResponseCode();
    if(responseCode == 200) {
        InputStream content = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(content, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line;
        while ((line = reader.readLine()) != null)
        {
            sb.append(line);
        }
        content.close();
    }
}catch (Exception ex) {
    Log.e(TAG, "Failed to send HTTP request due to: " + ex);
} finally {
    connection.disconnect();
}