android - How do I avoid network on main thread exception if I already use AsyncTask? -
it seems me m not calling url,network operations right mainactivity.
- i call url.connect() in class processdata
- i use doinbackground()
- i extend processdata class asynctask
but exception still occurs:
public class mainactivity extends actionbaractivity { private final string log_tag = mainactivity.class.getsimplename(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); processdata test = new processdata(); test.doinbackground("mysql") ; } //separate file processdata.java public class processdata extends asynctask<string, void, string> { public string createvalidurl(string s){ .....//code in method works fine } public string doinbackground(string ...params){ string validurl = createvalidurl(params[0]); httpurlconnection urlconnection = null; bufferedreader reader = null; url url = new url(validurl); urlconnection = (httpurlconnection) url.openconnection(); urlconnection.setrequestmethod("get"); urlconnection.connect(); inputstream inputstream = urlconnection.getinputstream(); stringbuffer buffer = new stringbuffer(); reader = new bufferedreader(new inputstreamreader(inputstream)); string line; while((line = reader.readline()) != null) { buffer.append(line + "\n"); } urlconnection.disconnect(); reader.close(); return buffer.tostring(); } }
the network main thread exception still occurs, read other posts. can wrap http connection code new thread, want find wrong
you have call
test.execute();
instead of
test.doinbackground("mysql") ;
Comments
Post a Comment