这是我第一次尝试让 Android 应用程序与存储在远程服务器上的 MySQL 数据库进行交互。我现在要做的就是将一条记录插入一个空的远程表中。基本上,我只是想建立一个连接并编写非常简单的数据。
我没有收到任何错误,但运行后我的表仍然是空的。我究竟做错了什么???
/my_db/test.php
include('connection.php');
$name='dummy';
$sql = "INSERT INTO test (name) VALUES ('$name')";
$result = mysql_query($sql);
if(!$result) {return -1;}
return 1;
?>
这是在我的 Activity 的 onCreate 函数中:
btnRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//Avoiding threading issues for the moment
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
DefaultHttpClient httpclient = new DefaultHttpClient();
String testURL = context.getResources().getString(R.string.dbURL) + "my_db/test.php";
System.err.println(testURL); //output looks good 'http://XXX.XXX.XXX.XXX/my_db/test.php'
HttpPost httppost = new HttpPost(testURL);
try
{
//not actually using the passed parameter for now
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", inputFullName.getText().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
System.err.println("done");
}
catch(Exception e)
{
System.err.println("error");
e.printStackTrace();
}
}
});