【发布时间】:2013-12-14 02:50:55
【问题描述】:
我正在构建 Android 应用程序,并希望将此数据提交到我的在线数据库中。它执行onPostExecute 方法,所以我知道它被调用了
但是,当我在数据库中查找该条目时,它并不存在。这是为什么呢?
任何帮助都将不胜感激。
异步任务
private class SendTheYak extends AsyncTask<String, String, String>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
/**
* Creating product
* */
@Override
protected String doInBackground(String... args) {
JSONParser jsonParser = new JSONParser();
String message = newYak.getText().toString();
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.dichoapp.com/Chatter/sendMessage.php");
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("userID", "William"));
params.add(new BasicNameValuePair("lat", "35.5"));
params.add(new BasicNameValuePair("long", "28.3"));
params.add(new BasicNameValuePair("distance", "2"));
params.add(new BasicNameValuePair("message", message));
params.add(new BasicNameValuePair("handle", "android"));
httppost.setEntity(new UrlEncodedFormEntity(params));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
Toast.makeText(getApplicationContext(), "It sent to DB", Toast.LENGTH_LONG).show();
}
}
php
<?php
$DB_HostName = "localhost";
$DB_Name = "xxx";
$DB_User = "xxx";
$DB_Pass = "xxx";
$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error());
mysql_select_db($DB_Name,$con) or die(mysql_error());
$userID = $_POST["userID"];
$lat = $_POST["lat"];
$long = $_POST["long"];
$distance = $_POST["distance"];
$message = $_POST["message"];
$message = mysql_real_escape_string($message);
$handle = $_POST["hndl"];
$handle = mysql_real_escape_string($handle);
$hidePin = $_POST["hidePin"];
if(empty($hidePin)){
$hidePin = "0";
}
if(empty($handle)){
mysql_query("INSERT INTO xxx (User_ID, Latitude, Longitude, Distance, Message, HidePin) VALUES ('$userID', '$lat', '$long', '$distance', '$message', '$hidePin')", $con);
$messageID = mysql_insert_id();
}else{
mysql_query("INSERT INTO xxx (User_ID, Latitude, Longitude, Distance, Message, Handle, HidePin) VALUES ('$userID', '$lat', '$long', '$distance', '$message', '$handle', '$hidePin')", $con);
$messageID = mysql_insert_id();
}
【问题讨论】:
-
收到什么错误?
-
没有收到错误。什么都没有被推入数据库表中
-
你能不能修改你的php脚本,在txt文件中写入日志,存储sql查询??
-
不知道怎么做,能给我一个链接作为参考吗?
-
我认为你应该使用
method.equals("POST")而不是method == "POST"