JToon
JToon

Reputation: 11

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. This code simply calls a PHP script on my web server, and reads the string it returns. I decided to update it to HttpURLConnection so the code could "get with the times".

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. This string is always something simple and short like "OK_1" , "ERR_TYPE_1" , "OK_2" , etc, etc .

So I tried updating this very simple code to an also very simple method using 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:

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

Note that it shows that the unexpected status line value is "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. 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.

Any help would be greatly appreciated.

EDIT: Logcat Exception print:

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

Upvotes: 1

Views: 2016

Answers (1)

RyuZz
RyuZz

Reputation: 581

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

Upvotes: 0

Related Questions