0

I want to check the HTTP response of a certain URL before loading into a webview. I only want to load webview if http response code is 200. This is a workaround for intercepting http errors. I have below:

HttpGet httpRequest = new HttpGet( "http://example.com");
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httpRequest);
int code = response.getStatusLine().getStatusCode();

But I encountered the following error:

java.lang.RuntimeException: Unable to start activity ComponentInfo
android.os.NetworkOnMainThreadException

How to fix it? Or any workaround to interept http errors in webview? Thanks

JR Galia
  • 17,229
  • 19
  • 92
  • 144

5 Answers5

4

android.os.NetworkOnMainThreadException occurs whenever you try to make long running tasks/process on Main UI Thread directly.

To resolve this issue, cover your webservice call inside AsyncTask. FYI, AsyncTask in android known as Painless Threading which means developer don't need to bother about Thread management. So Go and implement web API call or any long running tasks using AsyncTask, there are plenty of examples available on the web.

Update:

I only want to load webview if http response code is 200.

=> Based on your requirement, I would say include your code inside doInBackground() method and return status code value, Which you can check inside onPostExecute(). Now here you are getting status code value 200/201 then you can load WebView.

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • thanks... see my edit of what I'm trying to do, maybe u can help. – JR Galia May 21 '13 at 12:17
  • @JRGalia yes my answer is perfect as per your requirement. Write your web status checking call inside `doInBackground()` method and return status code to `onPostExecute()` where you can make comparison. – Paresh Mayani May 21 '13 at 12:20
3
class HTTPRequest extends AsyncTask<int, Void, void> {



    protected int doInBackground() {
        try {
            HttpGet httpRequest = new HttpGet( "http://example.com");
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httpRequest);
            int code = response.getStatusLine().getStatusCode();
            return code;
        } catch (Exception e) {
           e.printstacktrace();

        }
    }

    protected void onPostExecute(int code) {
        // TODO: check this.exception 
        // retrieve your 'code' here
    }
 }
DGN
  • 702
  • 3
  • 12
1

You are getting this Exception because you are carrying out a heavy Computation i.e Acessing Network in your case on UI Thread.

You should never do this .

Rather you can move this code to background Java Thread : Try :

private void doNetworkCompuation()
{
 new Thread(new Runnable() {

            @Override
            public void run() {

HttpGet httpRequest = new HttpGet( "http://example.com");

HttpClient httpclient = new DefaultHttpClient();

HttpResponse response = httpclient.execute(httpRequest);

int code = response.getStatusLine().getStatusCode();

}).start();
}
Usman Kurd
  • 7,212
  • 7
  • 57
  • 86
Nargis
  • 4,687
  • 1
  • 28
  • 45
  • i edited my question to reflect what Im trying to do. I want to check http response code before loading to webview, any workaround? – JR Galia May 21 '13 at 12:19
  • @JR dear I have already shown an exact way. Its now up to you whether want to ask for ready made code and wasting time their way. Go and try out at least the mentioned way. – Paresh Mayani May 21 '13 at 15:21
0

Try executing this code in Async Thread.

You can have a refrence from here: How to fix android.os.NetworkOnMainThreadException?

Community
  • 1
  • 1
Misha Bhardwaj
  • 1,377
  • 10
  • 14
0

You are not allowed to execute network requests on the main thread. You have to use a different thread for making this requests. You should use the AsyncTask, for an example look here.

Community
  • 1
  • 1
bogdan
  • 782
  • 3
  • 7