connection - Android app on real device,crashes when try connecting to server -
i trying run simple android code on real device keeps crashing, works fine on emulator.
my code
package com.example.new1; import java.io.bufferedreader; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.url; import android.os.asynctask; import android.os.bundle; import android.app.activity; import android.view.menu; import android.view.view; import android.widget.textview; public class mainactivity extends activity { textview tx; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); tx= (textview)findviewbyid(r.id.text); } public void func(view view) { //tx.settext("working fine till here."); new fetchsql().execute(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_main, menu); return true; } private class fetchsql extends asynctask<string,void,string> { @override protected string doinbackground(string... arg0) { url url = null; bufferedreader reader = null; stringbuilder stringbuilder; string myurl = "http://10.22.35.4:80/conc2.php"; try { url =new url(myurl); httpurlconnection connection = (httpurlconnection) url.openconnection(); connection.setrequestmethod("get"); connection.setreadtimeout(15*10000); connection.connect(); reader = new bufferedreader(new inputstreamreader(connection.getinputstream())); stringbuilder = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { stringbuilder.append(line + "\n"); } // todo auto-generated method stub return stringbuilder.tostring(); } catch(exception e) { tx.settext(e.tostring()); } return null; } protected void onpostexecute(string result) { tx.settext(result); } } }
it works fine on emulator , output gives
v. m. aroradr. c. p. reddyarti chowdharyjagdish singh on running same on device. gives following error code.
05-03 11:49:39.002: e/androidruntime(19934): fatal exception: asynctask #1 05-03 11:49:39.002: e/androidruntime(19934): java.lang.runtimeexception: error occured while executing doinbackground() 05-03 11:49:39.002: e/androidruntime(19934): @ android.os.asynctask$3.done(asynctask.java:200) 05-03 11:49:39.002: e/androidruntime(19934): @ java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:273) 05-03 11:49:39.002: e/androidruntime(19934): @ java.util.concurrent.futuretask.setexception(futuretask.java:124) 05-03 11:49:39.002: e/androidruntime(19934): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:307) 05-03 11:49:39.002: e/androidruntime(19934): @ java.util.concurrent.futuretask.run(futuretask.java:137) 05-03 11:49:39.002: e/androidruntime(19934): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1068) 05-03 11:49:39.002: e/androidruntime(19934): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:561) 05-03 11:49:39.002: e/androidruntime(19934): @ java.lang.thread.run(thread.java:1096) 05-03 11:49:39.002: e/androidruntime(19934): caused by: android.view.viewroot$calledfromwrongthreadexception: original thread created view hierarchy can touch views. 05-03 11:49:39.002: e/androidruntime(19934): @ android.view.viewroot.checkthread(viewroot.java:2811) 05-03 11:49:39.002: e/androidruntime(19934): @ android.view.viewroot.requestlayout(viewroot.java:594) 05-03 11:49:39.002: e/androidruntime(19934): @ android.view.view.requestlayout(view.java:8180) 05-03 11:49:39.002: e/androidruntime(19934): @ android.view.view.requestlayout(view.java:8180) 05-03 11:49:39.002: e/androidruntime(19934): @ android.view.view.requestlayout(view.java:8180) 05-03 11:49:39.002: e/androidruntime(19934): @ android.view.view.requestlayout(view.java:8180) 05-03 11:49:39.002: e/androidruntime(19934): @ android.widget.relativelayout.requestlayout(relativelayout.java:257) 05-03 11:49:39.002: e/androidruntime(19934): @ android.view.view.requestlayout(view.java:8180) 05-03 11:49:39.002: e/androidruntime(19934): @ android.widget.textview.checkforrelayout(textview.java:5383) 05-03 11:49:39.002: e/androidruntime(19934): @ android.widget.textview.settext(textview.java:2688) 05-03 11:49:39.002: e/androidruntime(19934): @ android.widget.textview.settext(textview.java:2556) 05-03 11:49:39.002: e/androidruntime(19934): @ android.widget.textview.settext(textview.java:2531) 05-03 11:49:39.002: e/androidruntime(19934): @ com.example.new1.mainactivity$fetchsql.doinbackground(mainactivity.java:66) 05-03 11:49:39.002: e/androidruntime(19934): @ com.example.new1.mainactivity$fetchsql.doinbackground(mainactivity.java:1) 05-03 11:49:39.002: e/androidruntime(19934): @ android.os.asynctask$2.call(asynctask.java:185) 05-03 11:49:39.002: e/androidruntime(19934): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:305) 05-03 11:49:39.002: e/androidruntime(19934): ... 4 more
please help, how run stupid code on real device. ive got device
gti-9003 , android 2.2.1 (froyo).. , in app i've put min sdk ..of 1.5(cupcake).
its because trying change ui, settext actually, in doinbackground method not meant ui tasks.
here:
catch(exception e) { tx.settext(e.tostring()); }
you can this:
catch(exception e) { return e.tostring(); }
and in onpostexecute
, set text textview
.
Comments
Post a Comment