EOFException getResponseCode()

Bla*_*gic 1 java android exception eof

我有这段代码:

public static boolean checkIfURLExists(String targetUrl) {
        HttpURLConnection httpUrlConn;
        System.setProperty("http.keepAlive", "false");
        try {
            httpUrlConn = (HttpURLConnection) new URL(targetUrl)
                    .openConnection();
            httpUrlConn.setRequestMethod("HEAD");

            // Set timeouts in milliseconds
            httpUrlConn.setConnectTimeout(500);
            httpUrlConn.setReadTimeout(1000);

            // Print HTTP status code/message for your information.
            return (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Removing the URL: " + targetUrl);
            return false;
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果URL可以访问,则测试.我用不同的URL多​​次调用这段代码.但是,代码在这一行给了我一个EOFException:

return (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK);
Run Code Online (Sandbox Code Playgroud)

例外:

12-18 11:11:57.655: W/System.err(30198): java.io.EOFException
12-18 11:11:57.655: W/System.err(30198):    at java.util.zip.GZIPInputStream.readFully(GZIPInputStream.java:206)
12-18 11:11:57.655: W/System.err(30198):    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:98)
12-18 11:11:57.665: W/System.err(30198):    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:81)
12-18 11:11:57.665: W/System.err(30198):    at libcore.net.http.HttpEngine.initContentStream(HttpEngine.java:528)
12-18 11:11:57.665: W/System.err(30198):    at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:836)
12-18 11:11:57.665: W/System.err(30198):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
12-18 11:11:57.665: W/System.err(30198):    at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
12-18 11:11:57.665: W/System.err(30198):    at com.cofely.gdfsuez.XmlDataParseHelper.checkIfURLExists(XmlDataParseHelper.java:172)
Run Code Online (Sandbox Code Playgroud)

有谁知道发生了什么,以及如何解决这个问题?谢谢

Bla*_*gic 8

哦,我刚刚找到了答案,显然这是新版android中的一个错误.添加此行适用于我:

httpUrlConn.setRequestProperty( "Accept-Encoding", "" ); 
Run Code Online (Sandbox Code Playgroud)

谢谢