1

I'm interested in just HTTP response codes (2xx, 3xx, 4xx & 5xx) for multiple sites.

My current code works well in the codename one simulator. But as app on Android the code throws EOFExceptions for some sites. In these cases I don't get a response code back.

How can I solve this issue? What's the best and recommended way to get all these http return codes in codename one?

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 Stack Trace:

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)
ryru
  • 61
  • 8
  • The `EOFException` seems only to appear when sending `HEAD` request. When using `GET` requests I get the proper response code. I would prefer to use `HEAD` requests because I'm just in the HTTP response code interested and don't need the HTTP body data. – ryru Feb 21 '16 at 18:48
  • Can you connect your device with a cable and launch the DDMS tool from the android SDK. Then add the stack trace for the `EOFException` to the question? – Shai Almog Feb 22 '16 at 04:08
  • Thank's Shai, here you go. I'm relatively new to mobile programming, hope I understand your request correctly. – ryru Feb 22 '16 at 20:21

2 Answers2

1

Do you have internet connection in mobile ? Is the host accessible with mobile?

tizbn
  • 1,907
  • 11
  • 16
  • Thanks tizbn. Yes I do have an active internet connection and the sites in question are accessible as well. – ryru Feb 22 '16 at 20:24
0

This is because of this known Android bug: Android's HttpURLConnection throws EOFException on HEAD requests

We'll add that workaround into Codename One so things should "just work".

Community
  • 1
  • 1
Shai Almog
  • 51,749
  • 5
  • 35
  • 65
  • Thanks Shai. Could you please let us know when you applied the workaround, so I can mark this answer as correct? Do you agree on the second part of the question, that the recommended way to read http response codes is within the `readResponse` with `setReadResponseForErrors(true)`? – ryru Feb 23 '16 at 15:11
  • Should be up now, new builds should include the fix. The `readResponseForErrors` means we will call `readResponse` even if there is an error code. I'm not sure if that makes a lot of sense for a `head` request but if it works for you then it should be fine. – Shai Almog Feb 24 '16 at 03:42
  • Thank you Shai for your fast help, seems to work perfectly fine now. Cause my use case is just the http response code, it makes sense to just request a `HEAD` message. – ryru Feb 25 '16 at 15:21