如何将数据从android发布到PHP(简单)?

时间:2014-01-12 13:37:59

标签: php android json http

我试图通过一种非常简单的方式从Android发送数据到PHP,但总是我收到错误。 请告诉我如何使它工作,我做错了什么?

PHP代码

<?php  
require 'connect.inc.php';
mysql_query("INSERT INTO 'names' VALUES ('2','".$_POST['name']."')");
?>

Android代码

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair(1);
    nameValuePairs.add(new BasicNameValuePair("name", "Alex"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
} catch(Eexceptions....){}

Log_cat

01-12 09:02:00.980: E/AndroidRuntime(942): FATAL EXCEPTION: main
01-12 09:02:00.980: E/AndroidRuntime(942): Process: com.example.httpjsonphp3, PID: 942
01-12 09:02:00.980: E/AndroidRuntime(942): java.lang.IllegalStateException: Could not execute method of the activity
01-12 09:02:00.980: E/AndroidRuntime(942):  at android.view.View$1.onClick(View.java:3814)
01-12 09:02:00.980: E/AndroidRuntime(942):  at android.view.View.performClick(View.java:4424)
01-12 09:02:00.980: E/AndroidRuntime(942):  at android.view.View$PerformClick.run(View.java:18383)
01-12 09:02:00.980: E/AndroidRuntime(942):  at android.os.Handler.handleCallback(Handler.java:733)
01-12 09:02:00.980: E/AndroidRuntime(942):  at android.os.Handler.dispatchMessage(Handler.java:95)
01-12 09:02:00.980: E/AndroidRuntime(942):  at android.os.Looper.loop(Looper.java:137)
01-12 09:02:00.980: E/AndroidRuntime(942):  at android.app.ActivityThread.main(ActivityThread.java:4998)
01-12 09:02:00.980: E/AndroidRuntime(942):  at java.lang.reflect.Method.invokeNative(Native Method)
01-12 09:02:00.980: E/AndroidRuntime(942):  at java.lang.reflect.Method.invoke(Method.java:515)
01-12 09:02:00.980: E/AndroidRuntime(942):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-12 09:02:00.980: E/AndroidRuntime(942):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-12 09:02:00.980: E/AndroidRuntime(942):  at dalvik.system.NativeStart.main(Native Method)
01-12 09:02:00.980: E/AndroidRuntime(942): Caused by: java.lang.reflect.InvocationTargetException
01-12 09:02:00.980: E/AndroidRuntime(942):  at java.lang.reflect.Method.invokeNative(Native Method)
01-12 09:02:00.980: E/AndroidRuntime(942):  at java.lang.reflect.Method.invoke(Method.java:515)
01-12 09:02:00.980: E/AndroidRuntime(942):  at android.view.View$1.onClick(View.java:3809)
01-12 09:02:00.980: E/AndroidRuntime(942):  ... 11 more
01-12 09:02:00.980: E/AndroidRuntime(942): Caused by: android.os.NetworkOnMainThreadException
01-12 09:02:00.980: E/AndroidRuntime(942):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
01-12 09:02:00.980: E/AndroidRuntime(942):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
01-12 09:02:00.980: E/AndroidRuntime(942):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-12 09:02:00.980: E/AndroidRuntime(942):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
01-12 09:02:00.980: E/AndroidRuntime(942):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
01-12 09:02:00.980: E/AndroidRuntime(942):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-12 09:02:00.980: E/AndroidRuntime(942):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-12 09:02:00.980: E/AndroidRuntime(942):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-12 09:02:00.980: E/AndroidRuntime(942):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-12 09:02:00.980: E/AndroidRuntime(942):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-12 09:02:00.980: E/AndroidRuntime(942):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-12 09:02:00.980: E/AndroidRuntime(942):  at com.example.httpjsonphp3.MainActivity.onSend(MainActivity.java:39)
01-12 09:02:00.980: E/AndroidRuntime(942):  ... 14 more
01-12 09:02:07.050: I/Process(942): Sending signal. PID: 942 SIG: 9

1 个答案:

答案 0 :(得分:0)

在我看来,我在代码中看到的错误是“Insert Into”指令中的表名不能在括号之间: 更正: 要求'connect.inc.php'; mysql_query(“INSERT INTO命名VALUES('2','”。$ _ POST ['name']。“')”); ?&GT;