1

I have a problem where my HttpsURLConnection will throw an EOFException when i try to open this URL: http://www.weather.com.cn/data/cityinfo/101210101.html

Code:

URL url = new URL("http://www.weather.com.cn/data/cityinfo/101210101.html");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream in = new BufferedInputStream(connection.getInputStream());
StringBuffer out = new StringBuffer();
byte[] b = new byte[1024];
for(int n;(n = in.read(b)) != -1;){
    out.append(new String(b, 0, n));
}

The following is the StackTrace:

java.io.EOFException
    at com.android.okio.RealBufferedSource.require(RealBufferedSource.java:64)
    at com.android.okio.RealBufferedSource.readIntLe(RealBufferedSource.java:115)
    at com.android.okio.GzipSource.consumeTrailer(GzipSource.java:168)
    at com.android.okio.GzipSource.read(GzipSource.java:87)
    at com.android.okio.RealBufferedSource$1.read(RealBufferedSource.java:168)
    at java.io.InputStream.read(InputStream.java:162)
    at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:149)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:295)
    at java.io.InputStream.read(InputStream.java:162)
    at com.coolweather.app.coolweather.util.HttpUtil$1.run(HttpUtil.java:47)
    at java.lang.Thread.run(Thread.java:818)
2

2 Answers 2

2

I have the same problem too. I have fixed it by adding :

connection.setRequestProperty("Accept-Encoding", "musixmatch");

or

connection.setRequestProperty("Accept-Encoding", "");

after connection.setRequestMethod("GET");

Setting connection.setRequestProperty("Accept-Encoding", ""); effectively fixed the issue because it doesn't wait anymore for gzipped output and so doesn't trigger a timeout.

more detail:https://code.google.com/p/android/issues/detail?id=24672

Sign up to request clarification or add additional context in comments.

1 Comment

I hope it'll help you.
0

You may add connection.setRequestProperty( "Accept-Encoding", "" ); after connection.setRequestMethod("GET"); because the http response header of the url is "Content-Encoding:gzip". You can see an explaination here and another here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.