我正在构建和Android应用程序,并希望将此数据提交到我的在线数据库。它执行onPostExecute
方法,所以我知道它被称为
然而,当我在我的数据库中查找条目时,它不在那里。这是为什么?
非常感谢任何帮助。
的AsyncTask
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();
}
答案 0 :(得分:0)
mysql_query("INSERT INTO xxx (User_ID, Latitude, Longitude, Distance, Message, HidePin) VALUES ('$userID', '$lat', '$long', '$distance', '$message', '$hidePin')", $con);
mysql_query函数出错。当你将它与连接变量一起使用时,它实际上是mysqli_query()
。
可替换地
如果您使用mysql_query
,请不要使用连接变量$con
。
试
mysql_query("INSERT INTO xxx (User_ID, Latitude, Longitude, Distance, Message, HidePin) VALUES ('$userID', '$lat', '$long', '$distance', '$message', '$hidePin')");
或
mysqli_query($con,"INSERT INTO xxx (User_ID, Latitude, Longitude, Distance, Message, HidePin) VALUES ('$userID', '$lat', '$long', '$distance', '$message', '$hidePin')");
答案 1 :(得分:0)
它没有工作,因为我没有使用我的硬编码值在数据库中满足我自己的外键约束。所以我只是把它变成了真实的数据,它就像一个冠军。
答案 2 :(得分:-1)
试试这个
在活动中使用
nameValuePairs2 = new ArrayList<NameValuePair>();
nameValuePairs2.add(new BasicNameValuePair("username",username));
nameValuePairs2.add(new BasicNameValuePair("password",password));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(mainurl+"registration.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs2));
HttpResponse response = httpclient.execute(httppost);
String the_string_response = convertResponseToString(response);
// Toast.makeText(getApplicationContext(), "Response " + the_string_response, Toast.LENGTH_LONG).show();
}catch(Exception e){
// Toast.makeText(getApplicationContext(), "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();
// System.out.println("Error in http connection "+e.toString());
}
并在php文件中
$sql="insert into login (username,password)values('".$_REQUEST['username']."','".$_REQUEST['password']."')";
$r=mysql_query($sql);
if(!$r)echo "Error in query: Record not submitted";
else echo "data submitted successfully";
mysql_close();
答案 3 :(得分:-2)
你从哪里获得JSONParser?我在Android APIs for JSON找不到它。如果你打印出从你的请求中收到的JSON,我猜你实际上并没有真正开始合法的电话。