我在LGE G2设备上遇到UrlConnection类问题。不知何故,相同的代码正在其他设备上运行。此外,当我将URL更改为另一个时,似乎DL正在工作。因此服务器部分可能存在问题。
我还使用4.4.2 android对Galaxy SIV上的相同URL进行了测试,看起来一切正常。
此外,我可以使用任何网络浏览器从同一网址获取文件...
这是堆栈跟踪:
04-17 12:17:35.681: W/System.err(10695): java.net.ProtocolException: Unexpected status line: HTTP/1.1
04-17 12:17:35.681: W/System.err(10695): at com.android.okhttp.internal.http.RawHeaders.setStatusLine(RawHeaders.java:108)
04-17 12:17:35.681: W/System.err(10695): at com.android.okhttp.internal.http.RawHeaders.fromBytes(RawHeaders.java:308)
04-17 12:17:35.681: W/System.err(10695): at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:135)
04-17 12:17:35.681: W/System.err(10695): at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:644)
04-17 12:17:35.681: W/System.err(10695): at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347)
04-17 12:17:35.681: W/System.err(10695): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
04-17 12:17:35.681: W/System.err(10695): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getHeaderField(HttpURLConnectionImpl.java:143)
04-17 12:17:35.681: W/System.err(10695): at java.net.URLConnection.getHeaderFieldInt(URLConnection.java:544)
04-17 12:17:35.681: W/System.err(10695): at java.net.URLConnection.getContentLength(URLConnection.java:316)
代码:
URL url = new URL(resource);
URLConnection connection = url.openConnection();
connection.setConnectTimeout(TEST_TIMEOUT);
connection.setReadTimeout(TEST_TIMEOUT);
int fileSize = connection.getContentLength();
InputStream inputStream = connection.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(
inputStream);
它是UrlConnection的标准用法。使用HttpUrlConnection或HttpsUrlConnection时也会发生相同的情况。正如我所说,这个错误只发生在LGE G2上。我在Nexus 4,Xperia Z1和Galaxy SIV上进行了测试,一切正常! BR, JDK