multithreading - NetworkOnMainThreadException Error in Android -


i'm trying use jsoup html parsing library html document using

document doc = jsoup.parse(u, 1000); 

and i'm getting error "android.os.networkonmainthreadexception"

i understand because need have download occurring somewhere other main thread don't understand how fix this.

if use threading need able return doc can parse through when download finished.

can me fix this?

the class using follows:

public class datasorter{     private document doc;    datasorter(){       downloaddata();    }    private void downloaddata() throws ioexception{         string url = "www.google.com";         url u = new url(url);         system.out.println("downloading....");         doc = jsoup.parse(u, 5000); //time out 5000ms         system.out.println("download successful");    }    document getdoc(){       return doc;    } } 

you doing network related operation on main ui thread. use thread or asynctask

http://developer.android.com/reference/android/os/networkonmainthreadexception.html

asynctask docs

http://developer.android.com/reference/android/os/asynctask.html

a similar post @

how fix android.os.networkonmainthreadexception?

you can use thread remember can update ui on ui thread , not on background thread.

you can use asynctask use onpreexecute , onpostexecute update ui. use doinbackground network related operaion.

move this

   document doc = jsoup.parse(u, 1000); 

inside thread or doinbackground of asynctask.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -