以代号

时间:2016-02-21 18:18:17

标签: codenameone

我对多个网站的HTTP响应代码(2xx,3xx,4xx和5xx)感兴趣。

我目前的代码在代号一个模拟器中运行良好。但是作为Android上的应用,代码会为某些网站引发EOFExceptions。在这些情况下,我没有收到回复代码。

我该如何解决这个问题?以代号为单位获取所有这些http返回码的最佳和推荐方法是什么?

for (final Host h : current_hosts) {

    ConnectionRequest cr = new ConnectionRequest() {


        protected void readResponse(InputStream input) {

            h.setHost_return_code(getResponseCode());

            try {
                String response = Util.readToString(input);
                input.close();
            } catch (IOException err) {
                // do nothing
            }

        }
    };
    cr.setUrl(h.getHost_url());
    cr.setHttpMethod("HEAD");
    cr.setFollowRedirects(false);
    cr.setReadResponseForErrors(true);
    cr.setPost(false);
    cr.setFailSilently(false);
    cr.setSilentRetryCount(2);
    cr.setTimeout(15000);

    NetworkManager.getInstance().addToQueueAndWait(cr);

}

DDMS堆栈跟踪:

02-22 17:04:19.463: W/System.err(29698): java.io.EOFException
02-22 17:04:19.463: W/System.err(29698):    at java.util.zip.GZIPInputStream.readFully(GZIPInputStream.java:202)
02-22 17:04:19.463: W/System.err(29698):    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:98)
02-22 17:04:19.463: W/System.err(29698):    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:81)
02-22 17:04:19.473: W/System.err(29698):    at com.android.okhttp.internal.http.HttpEngine.initContentStream(HttpEngine.java:468)
02-22 17:04:19.473: W/System.err(29698):    at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:666)
02-22 17:04:19.473: W/System.err(29698):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347)
02-22 17:04:19.473: W/System.err(29698):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
02-22 17:04:19.473: W/System.err(29698):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503)
02-22 17:04:19.473: W/System.err(29698):    at com.codename1.impl.android.AndroidImplementation.getResponseCode(AndroidImplementation.java:3947)
02-22 17:04:19.473: W/System.err(29698):    at com.codename1.io.ConnectionRequest.performOperation(ConnectionRequest.java:367)
02-22 17:04:19.473: W/System.err(29698):    at com.codename1.io.NetworkManager$NetworkThread.run(NetworkManager.java:269)
02-22 17:04:19.473: W/System.err(29698):    at com.codename1.impl.CodenameOneThread$1.run(CodenameOneThread.java:60)
02-22 17:04:19.473: W/System.err(29698):    at java.lang.Thread.run(Thread.java:841)

2 个答案:

答案 0 :(得分:1)

你有移动互联网连接吗? 主机是否可以通过移动设备访问?

答案 1 :(得分:0)

这是因为这个已知的Android错误:Android's HttpURLConnection throws EOFException on HEAD requests

我们将这个变通方法添加到Codename One中,因此事情应该“正常”。