java - Android problems with URL connection and threading, and IllegalStateExceptions -


this class should allow me connect url , retrieve data from. converts json data in string. maybe there problem here , can't see it. im pretty new sort of programming.

public class networkconnection extends asynctask<string, void, string> { private static final string debug_tag = "httpexample";     protected string doinbackground(string... urls) {     try{         return downloadurl(urls[0]);     } catch (ioexception e) {         return "unable retrieve web page";     } }  //fetch data url public string downloadurl(string myurl) throws ioexception {     inputstream = null;     int len = 500;     string text = new string();      try {         url url = new url(myurl);         httpurlconnection conn = (httpurlconnection) url.openconnection();         conn.setreadtimeout(10000 /* milliseconds */);         conn.setconnecttimeout(15000 /* milliseconds */);         conn.setrequestmethod("get");         conn.setdoinput(true);          // starts query         conn.connect();         int response = conn.getresponsecode();         log.d(debug_tag, "the response is: " + response);         = conn.getinputstream();          try {                     bufferedreader br = new bufferedreader(                             new inputstreamreader(                                     conn.getinputstream()));                     stringbuilder sb = new stringbuilder();                     string line;                     while ((line = br.readline()) != null) {                         sb.append(line + "\n");                     }                     br.close();                     jsonarray data = new jsonarray(sb.tostring());                     (int = 0; < data.length(); i++) {                         jsonobject row = data.getjsonobject(i);                         string klasse = row.getstring("k_nr");                         string raum = row.getstring("k_raum");                         text = text + klasse + ": " + raum + "\n";                     }         } catch (ioexception e) {          } catch (jsonexception e) {             e.printstacktrace();         }       } {         if (is != null) {             is.close();         }     }     return text; }  @override protected void onpostexecute(string s){     super.onpostexecute(s); } 

}

this class in main thread ui is. here try access url class networkconnection. problem don't know how thread correctly.

public class start extends activity { // public string  url = new string("http://10.0.2.2/?action=getklassen"); // public string  url = new string("http://127.0.0.1/webservice/ajax.php?action=getklassen"); public string  url; networkconnection netcon; public textview testausgabe;   @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.main);      intent = getintent();     string val = i.getstringextra("klasse");     textview ausgabe = (textview) findviewbyid(r.id.ausgabe);     testausgabe = (textview)findviewbyid(r.id.testausgabe);     netcon = new networkconnection();     url = new string("http://10.0.2.2/?action=getklassen");     ausgabe.settext(val);  }  public void myclickhandler(view view) {     connectivitymanager connmgr = (connectivitymanager)             getsystemservice(context.connectivity_service);     networkinfo networkinfo = connmgr.getactivenetworkinfo();     if (networkinfo != null && networkinfo.isconnected()) {         new networkconnection().execute(url);     } else {         testausgabe.settext("no network connection available.");     } }  public void onclick(view view){  }  public void zeigeliste_click (view view) {      startactivity(new intent(this, klassenview.class)); } public void ladedaten_click(view view) {     string data;      data = netcon.doinbackground(url);     testausgabe.settext(data); } 

}

here have exception thrown. when click on button should load data app crashes. has threading problem don't understand other exceptions. if great. in advance.

01-29 11:10:38.776 6648-6648/com.example.httptest e/androidruntime: fatal exception: main process: com.example.httptest, pid: 6648 java.lang.illegalstateexception: not execute method android:onclick  @ android.view.view$declaredonclicklistener.onclick(view.java:4452)  @ android.view.view.performclick(view.java:5198)  @ android.view.view$performclick.run(view.java:21147)  @ android.os.handler.handlecallback(handler.java:739)  @ android.os.handler.dispatchmessage(handler.java:95)  @ android.os.looper.loop(looper.java:148)  @ android.app.activitythread.main(activitythread.java:5417)  @ java.lang.reflect.method.invoke(native method)  @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726)  @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) caused by: java.lang.reflect.invocationtargetexception  @ java.lang.reflect.method.invoke(native method)  @ android.view.view$declaredonclicklistener.onclick(view.java:4447)  @ android.view.view.performclick(view.java:5198)   @ android.view.view$performclick.run(view.java:21147)   @ android.os.handler.handlecallback(handler.java:739)   @ android.os.handler.dispatchmessage(handler.java:95)   @ android.os.looper.loop(looper.java:148)   @ android.app.activitythread.main(activitythread.java:5417)   @ java.lang.reflect.method.invoke(native method)   @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726)   @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616)  caused by: android.os.networkonmainthreadexception  @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1273)  @ libcore.io.blockguardos.connect(blockguardos.java:110)  @ libcore.io.iobridge.connecterrno(iobridge.java:154)  @ libcore.io.iobridge.connect(iobridge.java:122)  @ java.net.plainsocketimpl.connect(plainsocketimpl.java:183)  @ java.net.plainsocketimpl.connect(plainsocketimpl.java:452)  @ java.net.socket.connect(socket.java:884)  @ com.android.okhttp.internal.platform.connectsocket(platform.java:117)  @ com.android.okhttp.internal.http.socketconnector.connectrawsocket(socketconnector.java:160)  @ com.android.okhttp.internal.http.socketconnector.connectcleartext(socketconnector.java:67)  @ com.android.okhttp.connection.connect(connection.java:152)  @ com.android.okhttp.connection.connectandsetowner(connection.java:185)  @ com.android.okhttp.okhttpclient$1.connectandsetowner(okhttpclient.java:128)  @ com.android.okhttp.internal.http.httpengine.nextconnection(httpengine.java:341)  @ com.android.okhttp.internal.http.httpengine.connect(httpengine.java:330)  @ com.android.okhttp.internal.http.httpengine.sendrequest(httpengine.java:248)  @ com.android.okhttp.internal.huc.httpurlconnectionimpl.execute(httpurlconnectionimpl.java:433)  @ com.android.okhttp.internal.huc.httpurlconnectionimpl.connect(httpurlconnectionimpl.java:114)  @ com.example.httptest.networkconnection.downloadurl(networkconnection.java:50)  @ com.example.httptest.networkconnection.doinbackground(networkconnection.java:29)  @ com.example.httptest.start.ladedaten_click(start.java:62)  @ java.lang.reflect.method.invoke(native method)   @ android.view.view$declaredonclicklistener.onclick(view.java:4447)   @ android.view.view.performclick(view.java:5198)   @ android.view.view$performclick.run(view.java:21147)   @ android.os.handler.handlecallback(handler.java:739)   @ android.os.handler.dispatchmessage(handler.java:95)   @ android.os.looper.loop(looper.java:148)   @ android.app.activitythread.main(activitythread.java:5417)   @ java.lang.reflect.method.invoke(native method)   @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726)   @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616)  

actually! made mistake in code. have use
netcon.execute(url); instead of netcon.doinbackground(url); , once execute() method called , clicked again start method before completing previous doinbackground() , exception occur again. 1 thing: inside method onpreexecute(), prevent button clicked again , onpostexecute(), make clickable again.

so doing way preventing create new thread. cheers!


Comments