i trying http post , came know time consuming tasks should used within async task or handler, tried code using handler getting handler uncaught exception, unable determine going wrong. below code , log trace.
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button bt = (button)findviewbyid(r.id.button1); button btx = (button)findviewbyid(r.id.button2); et = (edittext)findviewbyid(r.id.edittext1); etp = (edittext)findviewbyid(r.id.edittext2); bt.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { dialog = progressdialog.show(mainactivity.this,"loading", "please wait..."); h = new handler(){ @override public void handlemessage(message msg) { super.handlemessage(msg); dialog.dismiss(); } }; new thread(){ @override public void run() { super.run(); connection(); try { thread.sleep(3000); h.sendemptymessage(0); } catch (interruptedexception e) { e.printstacktrace(); } } }.start(); } }); // btx.setonclicklistener(sig); } private view.onclicklistener sig =new view.onclicklistener() { public void onclick(view v){ intent sign = new intent(mainactivity.this,signup.class); startactivity(sign); } }; public void connection(){ string = null; string x = et.gettext().tostring(); string y = etp.gettext().tostring(); if(x.length()>0 && y.length()>0){ = x; b = y; httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://animsinc.com/query.php"); try { list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2); namevaluepairs.add(new basicnamevaluepair("username", x)); namevaluepairs.add(new basicnamevaluepair("password",y)); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpclient.execute(httppost); // et.settext(""); // clear text box // etp.settext(""); // clear text box httpresponse responce = httpclient.execute(httppost); httpentity entity = responce.getentity(); = entityutils.tostring(entity); // in = entity.getcontent(); // log.e("post responce----->", "" + is); toast.maketext(this,""+is, toast.length_long).show(); } catch (clientprotocolexception e) { // todo auto-generated catch block }catch (unsupportedencodingexception e) { e.printstacktrace(); }catch (ioexception e) { // todo auto-generated catch block } } else{ // display message if text fields empty toast.maketext(getbasecontext(),"all field required",toast.length_short).show(); } if(is != null){ toast.maketext(this,""+is, toast.length_long).show(); } }
}
this log cat:
problem calling:
toast.maketext(this,""+is, toast.length_long).show();
from worker thread inside run() method. shouldn't it. can manipulate ui main(ui) thread.
solution use runonuithread() or asynctask or show toast in handler.
Comments
Post a Comment