2016-07-18 12 views
1

Ich habe ein Problem, wo mein HttpsURLConnection wird ein EOFException werfen, wenn ich versuche, diese URL zu öffnen: http://www.weather.com.cn/data/cityinfo/101210101.htmlAndroid HttpsURLConnection java.io.EOFException

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

Das Folgende ist die 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) 
+0

Erhalten Sie dieses Problem nur auf Android, weil es richtig als Java-Programm funktioniert? –

+0

wenn ich URL wie diese auf der gleichen Domain öffne, habe ich dieses Problem nicht bekommen: http://www.weather.com.cn/data/list3/city.xml http://www.weather.com .cn/data/list3/city19.xml –

Antwort

0

Sie können connection.setRequestProperty("Accept-Encoding", ""); nach connection.setRequestMethod("GET"); hinzufügen, da die hTTP-Antwort-Header der uRList. Sie können eine Erläuterung here und andere here sehen.

1

Ich habe das gleiche Problem auch. Ich habe es behoben durch Zugabe:

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

oder

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

nach connection.setRequestMethod("GET");

Einstellung connection.setRequestProperty ("Accept-Encoding", ""); Das Problem wurde effektiv behoben, da es nicht länger auf eine gezippte Ausgabe wartet und somit kein Timeout auslöst.

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

+0

Ich hoffe, es wird dir helfen. – Devin