2016-07-18 49 views
1

我有一個問題,即當我嘗試打開這個網址我HttpsURLConnection的將拋出EOFException類:http://www.weather.com.cn/data/cityinfo/101210101.html的Android HttpsURLConnection的java.io.EOFException的

代碼:

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

以下是堆棧跟蹤:

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) 
+0

你只能在Android得到這個問題,因爲它工作正常的java程序? –

+0

當我在同一個域上打開這樣的URL時,我沒有得到這個問題: http://www.weather.com.cn/data/list3/city.xml http://www.weather.com .cn/data/list3/city19.xml –

回答

0

您可以connection.setRequestMethod("GET");之後添加connection.setRequestProperty("Accept-Encoding", "");因爲URL的HTTP響應頭爲"Content-Encoding:gzip"。你可以看到一個解釋here 和另一個here

1

我也有同樣的問題。我已通過添加固定它:

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

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

connection.setRequestMethod("GET");

設置connection.setRequestProperty( 「接受編碼」, 「」);有效地解決了問題,因爲它不再等待gzip輸出,所以不會觸發超時。

更多詳細信息:https://code.google.com/p/android/issues/detail?id=24672

+0

我希望它能幫助你。 – Devin