android - Not connecting to wamp server and throwing NetworkOnMainThreadException -
i trying post data via android emulator php file on wamp server it's not connecting. keeps showing "unfortunately, creditswitch test has stopped."
below code. please need resolve now.
public class confirm_instant_top_up extends activity { textview network; textview names; textview amount; textview phone_number; textview email_address; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.confirm_instant_top_up); network = (textview) findviewbyid(r.id.network); names = (textview) findviewbyid(r.id.names); amount = (textview) findviewbyid(r.id.amount); phone_number = (textview) findviewbyid(r.id.phone_number); email_address = (textview) findviewbyid(r.id.email_address); intent intent = getintent(); final string ntwk = intent.getstringextra("network"); final string nms = intent.getstringextra("names"); final string amt = intent.getstringextra("amount"); final string phone = intent.getstringextra("phone"); final string email = intent.getstringextra("email"); network.settext(ntwk); names.settext(nms); amount.settext(amt); phone_number.settext(phone); email_address.settext(email); button btnok = (button) findviewbyid(r.id.btn_ok); btnok.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //intent intent = new intent(confirm_instant_top_up.this, confirm_instant_top_up.class); // check whether network field empty or not if(ntwk.length()>0) { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://10.0.2.2/test/confirmpayment.php"); try { list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2); namevaluepairs.add(new basicnamevaluepair("network", ntwk)); namevaluepairs.add(new basicnamevaluepair("names", nms)); namevaluepairs.add(new basicnamevaluepair("amount", amt)); namevaluepairs.add(new basicnamevaluepair("phone_number", phone)); namevaluepairs.add(new basicnamevaluepair("email_address", email)); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpclient.execute(httppost); toast.maketext(getbasecontext(),"sent",toast.length_short).show(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } else { //display message if text field empty toast.maketext(getbasecontext(),"all fields required",toast.length_short).show(); } } });; } }
below logcat
03-10 11:12:46.868: w/dalvikvm(2198): threadid=1: thread exiting uncaught exception (group=0xb3cf3b20) 03-10 11:12:46.958: e/androidruntime(2198): fatal exception: main 03-10 11:12:46.958: e/androidruntime(2198): process: com.creditswitchtest, pid: 2198 03-10 11:12:46.958: e/androidruntime(2198): android.os.networkonmainthreadexception 03-10 11:12:46.958: e/androidruntime(2198): @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1145) 03-10 11:12:46.958: e/androidruntime(2198): @ libcore.io.blockguardos.connect(blockguardos.java:84) 03-10 11:12:46.958: e/androidruntime(2198): @ libcore.io.iobridge.connecterrno(iobridge.java:127) 03-10 11:12:46.958: e/androidruntime(2198): @ libcore.io.iobridge.connect(iobridge.java:112) 03-10 11:12:46.958: e/androidruntime(2198): @ java.net.plainsocketimpl.connect(plainsocketimpl.java:192) 03-10 11:12:46.958: e/androidruntime(2198): @ java.net.plainsocketimpl.connect(plainsocketimpl.java:459) 03-10 11:12:46.958: e/androidruntime(2198): @ java.net.socket.connect(socket.java:843) 03-10 11:12:46.958: e/androidruntime(2198): @ org.apache.http.conn.scheme.plainsocketfactory.connectsocket(plainsocketfactory.java:119) 03-10 11:12:46.958: e/androidruntime(2198): @ org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator.java:144) 03-10 11:12:46.958: e/androidruntime(2198): @ org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.java:164) 03-10 11:12:46.958: e/androidruntime(2198): @ org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter.java:119) 03-10 11:12:46.958: e/androidruntime(2198): @ org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:360) 03-10 11:12:46.958: e/androidruntime(2198): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:555) 03-10 11:12:46.958: e/androidruntime(2198): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:487) 03-10 11:12:46.958: e/androidruntime(2198): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:465) 03-10 11:12:46.958: e/androidruntime(2198): @ com.creditswitchtest.confirm_instant_top_up$1.onclick(confirm_instant_top_up.java:74) 03-10 11:12:46.958: e/androidruntime(2198): @ android.view.view.performclick(view.java:4438) 03-10 11:12:46.958: e/androidruntime(2198): @ android.view.view$performclick.run(view.java:18422) 03-10 11:12:46.958: e/androidruntime(2198): @ android.os.handler.handlecallback(handler.java:733) 03-10 11:12:46.958: e/androidruntime(2198): @ android.os.handler.dispatchmessage(handler.java:95) 03-10 11:12:46.958: e/androidruntime(2198): @ android.os.looper.loop(looper.java:136) 03-10 11:12:46.958: e/androidruntime(2198): @ android.app.activitythread.main(activitythread.java:5017) 03-10 11:12:46.958: e/androidruntime(2198): @ java.lang.reflect.method.invokenative(native method) 03-10 11:12:46.958: e/androidruntime(2198): @ java.lang.reflect.method.invoke(method.java:515) 03-10 11:12:46.958: e/androidruntime(2198): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 03-10 11:12:46.958: e/androidruntime(2198): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 03-10 11:12:46.958: e/androidruntime(2198): @ dalvik.system.nativestart.main(native method)
well have done task copy , paste it.its complete task.if still got errors comment here.
public class confirm_instant_top_up extends activity { textview network; textview names; textview amount; textview phone_number; textview email_address; string ntwk; string nms; string amt; string phone; string email; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.confirm_instant_top_up); network = (textview) findviewbyid(r.id.network); names = (textview) findviewbyid(r.id.names); amount = (textview) findviewbyid(r.id.amount); phone_number = (textview) findviewbyid(r.id.phone_number); email_address = (textview) findviewbyid(r.id.email_address); intent intent = getintent(); ntwk = intent.getstringextra("network"); nms = intent.getstringextra("names"); amt = intent.getstringextra("amount"); phone = intent.getstringextra("phone"); email = intent.getstringextra("email"); network.settext(ntwk); names.settext(nms); amount.settext(amt); phone_number.settext(phone); email_address.settext(email); button btnok = (button) findviewbyid(r.id.btn_ok); btnok.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { backgroundtask task = new backgroundtask(); task.execute(); } }); class backgroundtask extends asynctask<string,void,void> { public backgroundtask() { super(); } @override protected void onpreexecute() { } @override protected void doinbackground(string...params) { if(ntwk.length()>0) { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://10.0.2.2/test/confirmpayment.php"); try { list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2); namevaluepairs.add(new basicnamevaluepair("network", ntwk)); namevaluepairs.add(new basicnamevaluepair("names", nms)); namevaluepairs.add(new basicnamevaluepair("amount", amt)); namevaluepairs.add(new basicnamevaluepair("phone_number", phone)); namevaluepairs.add(new basicnamevaluepair("email_address", email)); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpclient.execute(httppost); toast.maketext(getbasecontext(),"sent",toast.length_short).show(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } else { //display message if text field empty toast.maketext(getbasecontext(),"all fields required",toast.length_short).show(); } return null; } @override protected void onpostexecute() { return null; } } } }
also dont forget add permissions in androidmanifest.xml
<uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" />
Comments
Post a Comment