java - Android, Using an Async task to grab text from url -
right think far goes should consider me complete noob, in way on head.
i have attempted cobble understand several partial tutorials each dealing different aspect want do.
long , short of it, want text read url display in text view, understanding need asynctask since 3.0 update.
any on going wrong great don't understand what's causing error , how fix.
i know code contained within try , catch of doinbackground works on own before 3.0 update. whether needs modified work async don't know.
i imagine other issues arise once 1 sorted, if spots glaringly obvious grateful if point out.
string event; textview eventtext; textview titletext; string html; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.newevent); titletext = (textview) findviewbyid(r.id.title); eventtext = (textview) findviewbyid(r.id.event); new eventupdate().execute(); } public class eventupdate extends asynctask<string, void, void> { @override protected void doinbackground(string... url) { try { thread.sleep(4000); httpclient httpclient = new defaulthttpclient(); httpcontext localcontext = new basichttpcontext(); httpget httpget = new httpget( "http://masterzangetsu.eu/apps/rocksoctest"); // url! httpresponse response = httpclient.execute(httpget, localcontext); string result = ""; bufferedreader reader = new bufferedreader( new inputstreamreader(response.getentity().getcontent())); string line = null; while ((line = reader.readline()) != null) { result += line + "\n"; html = result; } } catch (interruptedexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } eventtext.settext("" + html); // todo auto-generated method stub return null; } }
and logcat error is:
w/dalvikvm(394): threadid=9: thread exiting uncaught exception (group=0x40015560) e/androidruntime(394): fatal exception: asynctask #1 e/androidruntime(394): java.lang.runtimeexception: error occured while executing doinbackground() e/androidruntime(394): @ android.os.asynctask$3.done(asynctask.java:200) e/androidruntime(394): @ java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:274) e/androidruntime(394): @ java.util.concurrent.futuretask.setexception(futuretask.java:125) e/androidruntime(394): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:308) e/androidruntime(394): @ java.util.concurrent.futuretask.run(futuretask.java:138) e/androidruntime(394): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1088) e/androidruntime(394): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:581) e/androidruntime(394): @ java.lang.thread.run(thread.java:1019) e/androidruntime(394): caused by: android.view.viewroot$calledfromwrongthreadexception: original thread created view hierarchy can touch views. e/androidruntime(394): @ android.view.viewroot.checkthread(viewroot.java:2932) e/androidruntime(394): @ android.view.viewroot.requestlayout(viewroot.java:629) e/androidruntime(394): @ android.view.view.requestlayout(view.java:8267) e/androidruntime(394): @ android.view.view.requestlayout(view.java:8267) e/androidruntime(394): @ android.view.view.requestlayout(view.java:8267) e/androidruntime(394): @ android.view.view.requestlayout(view.java:8267) e/androidruntime(394): @ android.widget.scrollview.requestlayout(scrollview.java:1291) e/androidruntime(394): @ android.view.view.requestlayout(view.java:8267) e/androidruntime(394): @ android.widget.relativelayout.requestlayout(relativelayout.java:257) e/androidruntime(394): @ android.view.view.requestlayout(view.java:8267) e/androidruntime(394): @ android.widget.textview.checkforrelayout(textview.java:5521) e/androidruntime(394): @ android.widget.textview.settext(textview.java:2724) e/androidruntime(394): @ android.widget.textview.settext(textview.java:2592) e/androidruntime(394): @ android.widget.textview.settext(textview.java:2567) e/androidruntime(394): @ com.masterzangetsu.kentrocksoc.nextevent$eventupdate.doinbackground(nextevent.java:81) e/androidruntime(394): @ com.masterzangetsu.kentrocksoc.nextevent$eventupdate.doinbackground(nextevent.java:1) e/androidruntime(394): @ android.os.asynctask$2.call(asynctask.java:185) e/androidruntime(394): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:306)
this line here
eventtext.settext("" + html);
is causing problem. can't update ui
doinbackground()
. has done in 1 of other methods or sent main activity
method. here in onpostexecute()
there different ways of accomplishing change asynctask
return result onpostexecute()
public class eventupdate extends asynctask<string, void, string> // change last param
then add onpostexecute()
method , change return
statement in doinbackground()
public class eventupdate extends asynctask<string, void, string> @override protected void onpostexecute(string result) { super.onpostexecute(result); eventtext.settext("" + result); } @override protected string doinbackground(string... url) { // work return html;
Comments
Post a Comment