php - Null exception in httprequest -
i trying values form mysql(xampp) database not working database name meeting_planner , table name participant
try { httppost httppost; httpclient httpclient; httpclient = new defaulthttpclient(); httppost = new httppost( "http://10.10.10.159/my_folder_inside_htdocs/getdata.php"); system.out.println("after url"); responsehandler<string> responsehandler = new basicresponsehandler(); system.out.println("after response handler"); final string response = httpclient.execute(httppost, responsehandler); system.out.println("the response "+response); log.e("pass 1", "connection success "); } catch (exception e) { e.printstacktrace(); }
getdata.php code here
<?php mysql_connect("localhost","admin","admin"); // host, username, password... mysql_select_db("meeting_planner"); // db name... $q=mysql_query("select * participant"); while($row=mysql_fetch_assoc($q)) $json_output[]=$row; print(json_encode($json_output)); mysql_close(); ?>
the response variable null in result of system.out.println("response"+response); log cat here
android.os.networkonmainthreadexception @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1145) @ libcore.io.blockguardos.connect(blockguardos.java:84) @ libcore.io.iobridge.connecterrno(iobridge.java:127) @ libcore.io.iobridge.connect(iobridge.java:112) @ java.net.plainsocketimpl.connect(plainsocketimpl.java:192) @ java.net.plainsocketimpl.connect(plainsocketimpl.java:459) @ java.net.socket.connect(socket.java:843) @ org.apache.http.conn.scheme.plainsocketfactory.connectsocket(plainsocketfactory.java:119) @
org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionopera
tor.java:144)
at org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.java:164) @ org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter.java:119) @ org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:360) @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:555) @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:653) @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:627) @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:616) @ com.example.androidmeetingplanner.invite.addlisteneronbutton(invite.java:130) @ com.example.androidmeetingplanner.invite.oncreate(invite.java:39) @ android.app.activity.performcreate(activity.java:5231) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) @ android.app.activitythread.access$800(activitythread.java:135) @ android.app.activitythread$h.handlemessage(activitythread.java:1196) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5017) @ java.lang.reflect.method.invokenative(native method)
you getting networkonmainthreadexception. please use asynctask make httprequest following:
private class httpgetter extends asynctask<void, void, void> { @override protected void doinbackground(url... urls) { try { httpclient httpclient = new defaulthttpclient(); httppost httppost= new httppost( "http://10.10.10.159/my_folder_inside_htdocs/getdata.php"); system.out.println("after url"); responsehandler<string> responsehandler = new basicresponsehandler(); system.out.println("after response handler"); string response = httpclient.execute(httppost,responsehandler); system.out.println("the response "+response); log.e("pass 1", "connection success "); } catch (exception e) { e.printstacktrace(); } return null; } }
and in activity write following code execute it
httpgetter getter = new httpgetter(); getter.execute(null,null);
Comments
Post a Comment