我正在尝试将数据发送到php文件并获取对我的android活动的响应。
我正在使用它将参数发送到php并从中接收:
String name = "snake", price = "10", description = "nada nada nada";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("price", price));
params.add(new BasicNameValuePair("description", description));
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.122/example/example.php");
// This is the data to send
try {
httppost.setEntity(new UrlEncodedFormEntity(params));
// Execute HTTP Post Request
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = httpclient.execute(httppost, responseHandler);
// This is the response from a php application
String reverseString = response;
Toast.makeText(this, "response" + reverseString, Toast.LENGTH_LONG).show();
} catch (ClientProtocolException e) {
Toast.makeText(this, "CPE response " + e.toString(),
Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
} catch (IOException e) {
Toast.makeText(this, "IOE response " + e.toString(),
Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
}
和我的php文件一样:
<?php
//code to reverse the string
$reversed = strrev($_POST["action"]);
echo $reversed;
?>
我认为问题是当我试图从php文件接收数据时。因为当我到达String response = httpclient.execute(httppost, responseHandler);
时我使用调试模式就是当我得到异常时。
logcat的:
06-20 10:19:39.173: E/AndroidRuntime(3580): FATAL EXCEPTION: main
06-20 10:19:39.173: E/AndroidRuntime(3580): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.login/com.example.plataformavideos.GraficosLinhas}: android.os.NetworkOnMainThreadException
06-20 10:19:39.173: E/AndroidRuntime(3580): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-20 10:19:39.173: E/AndroidRuntime(3580): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-20 10:19:39.173: E/AndroidRuntime(3580): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-20 10:19:39.173: E/AndroidRuntime(3580): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-20 10:19:39.173: E/AndroidRuntime(3580): at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 10:19:39.173: E/AndroidRuntime(3580): at android.os.Looper.loop(Looper.java:137)
06-20 10:19:39.173: E/AndroidRuntime(3580): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-20 10:19:39.173: E/AndroidRuntime(3580): at java.lang.reflect.Method.invokeNative(Native Method)
06-20 10:19:39.173: E/AndroidRuntime(3580): at java.lang.reflect.Method.invoke(Method.java:511)
06-20 10:19:39.173: E/AndroidRuntime(3580): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-20 10:19:39.173: E/AndroidRuntime(3580): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-20 10:19:39.173: E/AndroidRuntime(3580): at dalvik.system.NativeStart.main(Native Method)
06-20 10:19:39.173: E/AndroidRuntime(3580): Caused by: android.os.NetworkOnMainThreadException
06-20 10:19:39.173: E/AndroidRuntime(3580): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
06-20 10:19:39.173: E/AndroidRuntime(3580): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
06-20 10:19:39.173: E/AndroidRuntime(3580): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
06-20 10:19:39.173: E/AndroidRuntime(3580): at libcore.io.IoBridge.connect(IoBridge.java:112)
06-20 10:19:39.173: E/AndroidRuntime(3580): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
06-20 10:19:39.173: E/AndroidRuntime(3580): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
06-20 10:19:39.173: E/AndroidRuntime(3580): at java.net.Socket.connect(Socket.java:842)
06-20 10:19:39.173: E/AndroidRuntime(3580): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
06-20 10:19:39.173: E/AndroidRuntime(3580): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
06-20 10:19:39.173: E/AndroidRuntime(3580): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-20 10:19:39.173: E/AndroidRuntime(3580): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-20 10:19:39.173: E/AndroidRuntime(3580): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-20 10:19:39.173: E/AndroidRuntime(3580): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-20 10:19:39.173: E/AndroidRuntime(3580): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
06-20 10:19:39.173: E/AndroidRuntime(3580): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
06-20 10:19:39.173: E/AndroidRuntime(3580): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
06-20 10:19:39.173: E/AndroidRuntime(3580): at com.example.plataformavideos.GraficosLinhas.postData(GraficosLinhas.java:91)
06-20 10:19:39.173: E/AndroidRuntime(3580): at com.example.plataformavideos.GraficosLinhas.onCreate(GraficosLinhas.java:70)
06-20 10:19:39.173: E/AndroidRuntime(3580): at android.app.Activity.performCreate(Activity.java:5104)
06-20 10:19:39.173: E/AndroidRuntime(3580): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-20 10:19:39.173: E/AndroidRuntime(3580): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-20 10:19:39.173: E/AndroidRuntime(3580): ... 11 more
答案 0 :(得分:1)
首先检查您的设备是否可以连接到本地网络
<强> NetworkOnMainThreadException
强>
当应用程序尝试在其主线程上执行网络操作时引发的异常。
http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
更喜欢使用 AsynkTask
进行网络运营 ...
修改强>
您需要使用您的代码进行一些更改.......
将您的代码放入 ASYNKTASK
http://developer.android.com/reference/android/os/AsyncTask.html
更改此
List<NameValuePair> params = new ArrayList<NameValuePair>();
这个
List<NameValuePair> params = new ArrayList<NameValuePair>(3);
并且您将变量传递给PHP服务器(名称,价格,描述)确保您的 PHP代码文件
中有3个变量