HTTP Response in Android - NetworkOnMainThreadException -


this question has answer here:

i want check http response of url before loading webview. want load webview if http response code 200. workaround intercepting http errors. have below:

httpget httprequest = new httpget( "http://example.com"); httpclient httpclient = new defaulthttpclient(); httpresponse response = httpclient.execute(httprequest); int code = response.getstatusline().getstatuscode(); 

but encountered following error:

java.lang.runtimeexception: unable start activity componentinfo android.os.networkonmainthreadexception 

how fix it? or workaround interept http errors in webview? thanks

android.os.networkonmainthreadexception occurs whenever try make long running tasks/process on main ui thread directly.

to resolve issue, cover webservice call inside asynctask. fyi, asynctask in android known painless threading means developer don't need bother thread management. go , implement web api call or long running tasks using asynctask, there plenty of examples available on web.

update:

i want load webview if http response code 200.

=> based on requirement, include code inside doinbackground() method , return status code value, can check inside onpostexecute(). here getting status code value 200/201 can load webview.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -