Tarun Sharma
Tarun Sharma

Reputation: 599

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

Upvotes: 0

Views: 2669

Answers (2)

Flavio Battocchio
Flavio Battocchio

Reputation: 75

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)

Upvotes: 4

Patrick Dorn
Patrick Dorn

Reputation: 768

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

Upvotes: 1

Related Questions