简体   繁体   English

Android HttpURLConnection-意外的状态行:

[英]Android HttpURLConnection - Unexpected status line:

I've been struggling with a problem for a while now which has been driving me nuts. 我一直在为一个问题苦苦挣扎,这让我发疯了。

I Have some (working) code using the deprecated methods of HttpClient. 我有一些(使用中)使用HttpClient弃用方法的代码。 This code simply calls a PHP script on my web server, and reads the string it returns. 此代码仅在我的Web服务器上调用一个PHP脚本,并读取它返回的字符串。 I decided to update it to HttpURLConnection so the code could "get with the times". 我决定将其更新为HttpURLConnection,以便代码可以“与时俱进”。

Here is the working , deprecated code: 这是不起作用的有效代码:

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

Simple stuff, works fine. 简单的东西,很好用。

This code is meant to call a PHP script that keeps returning a simple string. 该代码旨在调用一个PHP脚本,该脚本一直返回简单的字符串。 This string is always something simple and short like "OK_1" , "ERR_TYPE_1" , "OK_2" , etc, etc . 此字符串始终是简单而简短的名称,例如“ OK_1”,“ ERR_TYPE_1”,“ OK_2”等。

So I tried updating this very simple code to an also very simple method using HttpURLConnection . 因此,我尝试使用HttpURLConnection将这个非常简单的代码更新为一个也非常简单的方法。 I have tried a number of variations, but the gist is this: 我尝试了多种变体,但是要点是:

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

No matter what variations I run, when I try to call getInputStream I ALWAYS Unexpected status line: 无论运行什么变体,当我尝试调用getInputStream时,我总是会出现意外状态行:

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

Note that it shows that the unexpected status line value is "empty" . 请注意,这表明意外的状态行值为“ empty”。

I realize this is a problem that might be server side related, but my php script will always only return a single string like i mentioned. 我意识到这可能是服务器端相关的问题,但是我的php脚本始终只会返回一个字符串,就像我提到的那样。 Is there something i can do on my side? 有什么我可以做的吗? I Would really appreciate any tips because I'm about to give up and go back to HttpClient which is working just fine despite being a deprecated method. 我真的很感激任何提示,因为我将放弃并返回到HttpClient,尽管该方法已被弃用,但仍可以正常工作。

Any help would be greatly appreciated. 任何帮助将不胜感激。

EDIT: Logcat Exception print: 编辑:Logcat异常打印:

11-26 08:57:02.602 1109-1954/app.mission.manager W/System.err: java.net.ProtocolException: Unexpected status line: 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(HttpURL 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:位于com.android.okhttp。 internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:101)11-26 08:57:02.603 1109-1954 / app.mission.manager W / System.err:位于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(HttpURL ConnectionImpl.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 11 ConnectionImpl.java:341)11-26 08:57:02.603 1109-1954 / app.mission.manager W / System.err:在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。管理器W / System.err:在android.os.AsyncTask $ 2.call(AsyncTask.java:292)11-26 08:57:02.603 1109-1954 / app.mission.manager W / System.err:在java.util .concurrent.FutureTask.run(FutureTask.java:237)11-26 08:57:02.603 1109-1954 / app.mission.manager W / System.err:在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 11 09-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) 09-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。管理器W / System.err:位于java.lang.Thread.run(Thread.java:818)

You can try this code, if it's still not working I'm sure it's a server side problem. 您可以尝试使用此代码,如果仍然无法运行,我确定这是服务器端的问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM