VM4
VM4

Reputation: 6499

HttpURLConnection and no internet

Don't know if I'm the first one to run into this, but I'll post it here to save someones time. Ok, so after playing around and trying out HttpURLConnection to do some HTTP requests from Android I wondered what would happen if I used it with Internet on my phone disabled. I've stumbled on an interesting bug:

    URL url = new URL("http://google.com");
    HttpURLConnection c = (HttpURLConnection) url.openConnection();
    c.setReadTimeout(15000);
    try {
        InputStream in = new BufferedInputStream(c.getInputStream());
        httpResult = readStream(in);
    } catch (IOException e) {
        Log.e(TAG, "Error: ", e);

    } finally {
        c.disconnect();
    }

If I run this code with Internet disabled on my phone an IOException gets caught with no stacktrace whatsoever. What's happenning?

Upvotes: 1

Views: 2848

Answers (1)

VM4
VM4

Reputation: 6499

So after some digging around I found that a subtype of IOException is actually being thrown: UnknownHostException. Android documentation for HttpURLConnection.getInputStream() and HttpURLConnection.connect() say nothing about it.

Upvotes: 3

Related Questions