json - Couldn't fix Future Task exception in android -
i'm pretty new in android programming, , i'm trying write piece of codes include httppost,json,asynctask etc. i'm getting error couldn't fix it.
public void onclick(view v) { mainactivity.this.runonuithread(new runnable() { public void run() { requestloginfromserver rl = new requestloginfromserver(); rl.execute().tostring(); } }); } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } public class requestloginfromserver extends asynctask<object, object, object> { textview txt = (textview) findviewbyid(r.id.textview1); edittext edt = (edittext) findviewbyid(r.id.edittext1); @override protected object doinbackground(object... params) { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost( "http://shopstable.turkcell.com.tr/timmenu/getperosconfig.do"); httppost.setheader("content-type", "application/json"); try { httpresponse response = httpclient.execute(httppost); httpentity resentity = response.getentity(); = resentity.getcontent(); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); // strjson = null; } try { bufferedreader reader = new bufferedreader( new inputstreamreader(is, "utf-8"), 8); // iso-8859-1 // stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "/n"); } is.close(); strjson = sb.tostring(); } catch (exception e) { } try { if (strjson != null) { jobj = new jsonobject(strjson); string text = jobj.get("masterperosactive").tostring(); edt.settext(text); } else { jobj = null; } } catch (jsonexception e) { } return "executed"; }
and exception -log cat:
07-22 09:51:36.791: e/androidruntime(13010): fatal exception: asynctask #1 07-22 09:51:36.791: e/androidruntime(13010): java.lang.runtimeexception: error occured while executing doinbackground() 07-22 09:51:36.791: e/androidruntime(13010): @ android.os.asynctask$3.done(asynctask.java:299) 07-22 09:51:36.791: e/androidruntime(13010): @ java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:273) 07-22 09:51:36.791: e/androidruntime(13010): @ java.util.concurrent.futuretask.setexception(futuretask.java:124) 07-22 09:51:36.791: e/androidruntime(13010): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:307) 07-22 09:51:36.791: e/androidruntime(13010): @ java.util.concurrent.futuretask.run(futuretask.java:137) 07-22 09:51:36.791: e/androidruntime(13010): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) 07-22 09:51:36.791: e/androidruntime(13010): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) 07-22 09:51:36.791: e/androidruntime(13010): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) 07-22 09:51:36.791: e/androidruntime(13010): @ java.lang.thread.run(thread.java:882) 07-22 09:51:36.791: e/androidruntime(13010): caused by: android.view.viewrootimpl$calledfromwrongthreadexception: original thread created view hierarchy can touch views. 07-22 09:51:36.791: e/androidruntime(13010): @ android.view.viewrootimpl.checkthread(viewrootimpl.java:4634) 07-22 09:51:36.791: e/androidruntime(13010): @ android.view.viewrootimpl.invalidatechildinparent(viewrootimpl.java:867) 07-22 09:51:36.791: e/androidruntime(13010): @ android.view.viewgroup.invalidatechild(viewgroup.java:4066) 07-22 09:51:36.791: e/androidruntime(13010): @ android.view.view.invalidate(view.java:10193) 07-22 09:51:36.791: e/androidruntime(13010): @ android.widget.textview.invalidateregion(textview.java:4375) 07-22 09:51:36.791: e/androidruntime(13010): @ android.widget.textview.invalidatecursor(textview.java:4318) 07-22 09:51:36.791: e/androidruntime(13010): @ android.widget.textview.spanchange(textview.java:7172) 07-22 09:51:36.791: e/androidruntime(13010): @ android.widget.textview$changewatcher.onspanadded(textview.java:8759) 07-22 09:51:36.791: e/androidruntime(13010): @ android.text.spannablestringbuilder.sendspanadded(spannablestringbuilder.java:979) 07-22 09:51:36.791: e/androidruntime(13010): @ android.text.spannablestringbuilder.setspan(spannablestringbuilder.java:688) 07-22 09:51:36.791: e/androidruntime(13010): @ android.text.spannablestringbuilder.setspan(spannablestringbuilder.java:588) 07-22 09:51:36.791: e/androidruntime(13010): @ android.text.selection.setselection(selection.java:76) 07-22 09:51:36.791: e/androidruntime(13010): @ android.text.selection.setselection(selection.java:87) 07-22 09:51:36.791: e/androidruntime(13010): @ android.text.method.arrowkeymovementmethod.initialize(arrowkeymovementmethod.java:302) 07-22 09:51:36.791: e/androidruntime(13010): @ android.widget.textview.settext(textview.java:3535) 07-22 09:51:36.791: e/androidruntime(13010): @ android.widget.textview.settext(textview.java:3405) 07-22 09:51:36.791: e/androidruntime(13010): @ android.widget.edittext.settext(edittext.java:80) 07-22 09:51:36.791: e/androidruntime(13010): @ android.widget.textview.settext(textview.java:3380) 07-22 09:51:36.791: e/androidruntime(13010): @ com.example.test2.mainactivity$requestloginfromserver.doinbackground(mainactivity.java:113) 07-22 09:51:36.791: e/androidruntime(13010): @ android.os.asynctask$2.call(asynctask.java:287) 07-22 09:51:36.791: e/androidruntime(13010): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:305) 07-22 09:51:36.791: e/androidruntime(13010): ... 5 more 07-22 09:51:36.851: w/iinputconnectionwrapper(13010): showstatusicon on inactive inputconnection 07-22 09:53:22.261: d/activitythread(13141): handleresumeactivity pri:0 07-22 09:53:22.261: d/activitythread(13141): handleresumeactivity set pri:0 07-22 09:53:27.531: w/dalvikvm(13141): threadid=11: thread exiting uncaught exception (group=0x40d9a318) 07-22 09:53:27.561: e/androidruntime(13141): fatal exception: asynctask #1 07-22 09:53:27.561: e/androidruntime(13141): java.lang.runtimeexception: error occured while executing doinbackground() 07-22 09:53:27.561: e/androidruntime(13141): @ android.os.asynctask$3.done(asynctask.java:299) 07-22 09:53:27.561: e/androidruntime(13141): @ java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:273) 07-22 09:53:27.561: e/androidruntime(13141): @ java.util.concurrent.futuretask.setexception(futuretask.java:124) 07-22 09:53:27.561: e/androidruntime(13141): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:307) 07-22 09:53:27.561: e/androidruntime(13141): @ java.util.concurrent.futuretask.run(futuretask.java:137) 07-22 09:53:27.561: e/androidruntime(13141): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) 07-22 09:53:27.561: e/androidruntime(13141): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) 07-22 09:53:27.561: e/androidruntime(13141): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) 07-22 09:53:27.561: e/androidruntime(13141): @ java.lang.thread.run(thread.java:882) 07-22 09:53:27.561: e/androidruntime(13141): caused by: android.view.viewrootimpl$calledfromwrongthreadexception: original thread created view hierarchy can touch views. 07-22 09:53:27.561: e/androidruntime(13141): @ android.view.viewrootimpl.checkthread(viewrootimpl.java:4634) 07-22 09:53:27.561: e/androidruntime(13141): @ android.view.viewrootimpl.invalidatechildinparent(viewrootimpl.java:867) 07-22 09:53:27.561: e/androidruntime(13141): @ android.view.viewgroup.invalidatechild(viewgroup.java:4066) 07-22 09:53:27.561: e/androidruntime(13141): @ android.view.view.invalidate(view.java:10193) 07-22 09:53:27.561: e/androidruntime(13141): @ android.widget.textview.invalidateregion(textview.java:4375) 07-22 09:53:27.561: e/androidruntime(13141): @ android.widget.textview.invalidatecursor(textview.java:4318) 07-22 09:53:27.561: e/androidruntime(13141): @ android.widget.textview.spanchange(textview.java:7172) 07-22 09:53:27.561: e/androidruntime(13141): @ android.widget.textview$changewatcher.onspanadded(textview.java:8759) 07-22 09:53:27.561: e/androidruntime(13141): @ android.text.spannablestringbuilder.sendspanadded(spannablestringbuilder.java:979) 07-22 09:53:27.561: e/androidruntime(13141): @ android.text.spannablestringbuilder.setspan(spannablestringbuilder.java:688) 07-22 09:53:27.561: e/androidruntime(13141): @ android.text.spannablestringbuilder.setspan(spannablestringbuilder.java:588) 07-22 09:53:27.561: e/androidruntime(13141): @ android.text.selection.setselection(selection.java:76) 07-22 09:53:27.561: e/androidruntime(13141): @ android.text.selection.setselection(selection.java:87) 07-22 09:53:27.561: e/androidruntime(13141): @ android.text.method.arrowkeymovementmethod.initialize(arrowkeymovementmethod.java:302) 07-22 09:53:27.561: e/androidruntime(13141): @ android.widget.textview.settext(textview.java:3535) 07-22 09:53:27.561: e/androidruntime(13141): @ android.widget.textview.settext(textview.java:3405) 07-22 09:53:27.561: e/androidruntime(13141): @ android.widget.edittext.settext(edittext.java:80) 07-22 09:53:27.561: e/androidruntime(13141): @ android.widget.textview.settext(textview.java:3380) 07-22 09:53:27.561: e/androidruntime(13141): @ com.example.test2.mainactivity$requestloginfromserver.doinbackground(mainactivity.java:111) 07-22 09:53:27.561: e/androidruntime(13141): @ android.os.asynctask$2.call(asynctask.java:287) 07-22 09:53:27.561: e/androidruntime(13141): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:305) 07-22 09:53:27.561: e/androidruntime(13141): ... 5 more
caused caused by: android.view.viewrootimpl$calledfromwrongthreadexception: original thread created view hierarchy can touch views.
. performing changes on ui non-ui thread. @ least line here (in asynctask doinbackground
):
string text = jobj.get("masterperosactive").tostring(); edt.settext(text);
since using asynctask, why not perform ui changes in onpostexecute()
method requestloginserver
class change (only last lines touched):
public class requestloginfromserver extends asynctask<object, object, string> { textview txt = (textview) findviewbyid(r.id.textview1); edittext edt = (edittext) findviewbyid(r.id.edittext1); @override protected object doinbackground(object... params) { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://shopstable.turkcell.com.tr/timmenu/getperosconfig.do"); httppost.setheader("content-type", "application/json"); try { httpresponse response = httpclient.execute(httppost); httpentity resentity = response.getentity(); = resentity.getcontent(); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); // strjson = null; } try { bufferedreader reader = new bufferedreader(new inputstreamreader(is, "utf-8"), 8); // iso-8859-1 // stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "/n"); } is.close(); strjson = sb.tostring(); } catch (exception e) { } try { if (strjson != null) { jobj = new jsonobject(strjson); string text = jobj.get("masterperosactive").tostring(); return text; } else { jobj = null; } } catch (jsonexception e) { } return null; } protected void onpostexecute(string result) { if(result != null) { edt.settext(result); } else { // show message/label went wrong } } }
Comments
Post a Comment