简体   繁体   中英

Unexpected status line exception using HttpUrlConnection in android?

I have developed an Android app which sends/gets some data to/from server. The app works fine, however, on Android KitKat 4.4.2 the app throws an exception:

"java.net.ProtocolException: Unexpected status line: ".

Please tell me how to get rid of it and why this is happening

HttpURLConnection.setFollowRedirects(true);
    con = (HttpURLConnection) new URL(url[0]).openConnection();
    con.setRequestMethod("GET");
    con.setConnectTimeout(10000);

I was having same problem. Looks that was due to recycling connection.

con.setRequestProperty("Connection", "close");

did the trick (Or use addHeader, if still using Apache Http)

Maybe OkHttp solves your problem:

http://square.github.io/okhttp/

You can try out OkHttp without rewriting your network code. The okhttp-urlconnection module implements the familiar java.net.HttpURLConnection API and the okhttp-apache module implements the Apache HttpClient API.

Note: OkHttp is standard in Android 5. So why not use it in lower versions :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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