我试图从mysql(xampp)数据库获取值,但它无法正常工作 我的数据库名称是meeting_planner,表名是参与者
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 is "+response);
Log.e("pass 1", "connection success ");
} catch (Exception e) {
e.printStackTrace();
}
getdata.php代码在这里
<?php
mysql_connect("localhost","admin","admin"); // host, username, password...
mysql_select_db("meeting_planner"); // db name...
$q=mysql_query("SELECT * FROM participant");
while($row=mysql_fetch_assoc($q))
$json_output[]=$row;
print(json_encode($json_output));
mysql_close();
?>
响应变量在System.out.println的结果中为空(&#34;响应&#34; +响应); log cat就在这里
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
at libcore.io.IoBridge.connect(IoBridge.java:112)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
at java.net.Socket.connect(Socket.java:843)
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
at
org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOpera
tor.java:144)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
at com.example.androidmeetingplanner.Invite.addListenerOnButton(Invite.java:130)
at com.example.androidmeetingplanner.Invite.onCreate(Invite.java:39)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
答案 0 :(得分:0)
android.os.NetworkOnMainThreadException表示你在GUI线程中调用请求。您应该在另一个线程中使用网络执行所有操作。
例如,您可以将此代码添加到AsyncTask。
答案 1 :(得分:0)
您正在获取NetworkOnMainThreadException。请使用AsyncTask来生成HttpRequest,如下所示:
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 is "+response);
Log.e("pass 1", "connection success ");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
在您的活动中编写以下代码来执行它
HttpGetter getter = new HttpGetter();
getter.execute(null,null);
答案 2 :(得分:0)
请查看此参考:How to fix android.os.NetworkOnMainThreadException?。 Android不能让你在主线程上发出http请求,你应该在不同的线程上发出请求