【问题标题】:how to post data from android to PHP(simple)?如何将数据从 android 发布到 PHP(简单)?
【发布时间】:2014-01-12 13:37:59
【问题描述】:

我正在尝试通过一种非常简单的方式从 Android 向 PHP 发送数据,但总是出错。 请告诉我如何让它工作,我做错了什么?

PHP 代码

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

安卓代码

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

【问题讨论】:

  • 什么/哪里是错误?在 php 或 android 代码中?记录?
  • 你的catch块在try块里面吗?我对 android 了解不多,但这不是你在 Java 中捕获异常的方式。
  • 我已经放了logcat数据@StarsSky

标签: php android json http


【解决方案1】:

在我看来,我在您的代码中看到的一个错误是“插入”指令中的表名不能放在括号内: 更正: 需要'connect.inc.php'; mysql_query("INSERT INTO names VALUES ('2','".$_POST['name']."')"); ?>

【讨论】:

  • 不,这不是问题,我只是在示例中这样做了:``stackoverflow 以不同的方式理解
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多