我想将我的Android应用程序中的一些数据保存到服务器上的mysql数据库中。问题是一切似乎都运行得很好,我收到的消息是我的数据已成功发送但我的数据库没有收到任何内容。 我有以下文件:
main.java
// setup a dialog window
alertDialogBuilder.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Drawing the marker in the Google Maps
drawMarker(point);
// Drawing the circle
drawCircle(point);
//Add proximity alert
addProximAlert(point);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
InputStream is = null;
// add values to the database
String name = "" + editText1.getText();
String description = "" + editText2.getText();
String latitude = "" + Double.toString(point.latitude);
String longitude = "" + Double.toString(point.longitude);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("description", description));
nameValuePairs.add(new BasicNameValuePair("latitude", latitude));
nameValuePairs.add(new BasicNameValuePair("longitude", longitude));
nameValuePairs.add(new BasicNameValuePair("user","Someone"));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("XXXXX/data2.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
String msg = "Data has been sent successfully";
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
} catch (ClientProtocolException e) {
Log.e("ClientProtocol", "Log_Tag");
e.printStackTrace();
String msg2 = "Log_Tag";
Toast.makeText(getApplicationContext(), msg2, Toast.LENGTH_LONG).show();
} catch (IOException e) {
Log.e("Log_Tag", "IOException");
e.printStackTrace();
String msg3 = "IOException";
Toast.makeText(getApplicationContext(), msg3, Toast.LENGTH_LONG).show();
}
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
和我的php文件
<?php
function createNewProduct() {
$response = array();
$name=$_POST['name'];
$description=$_POST['description'];
$latitude=$_POST['latitude'];
$longitude=$_POST['longitude'];
$user=$_POST['user'];
//$db = new DbConnect();
$connection = mysqli_connect('localhost', 'XXXXXXXXX', 'XXXX','XXXX');
$query = "INSERT INTO pois(name,description,lat,longit,created_by) VALUES('$name','$description','$latitude','$longitude','$user')";
$result = mysqli_query($connection, $query) or die(mysql_error());
if ($result) {
$response["error"] = false;
$response["message"] = "OK!";
} else {
$response["error"] = true;
$response["message"] = "Failed!";
}
// echo json response
echo json_encode($response);
}
createNewProduct();
?>
任何想法/提示可能是错误?
编辑: 感谢大家的帮助!我发现错误是什么。说这个是一种耻辱,但我在我的php文件中发送了错误的密码。现在一切都有效。
答案 0 :(得分:0)
您可能想要测试PHP,使用工具将数据发送到php(例如cURL),并检查您的服务器是否首先按预期运行。
然后,检查你的android是否可以正确
- (IBAction)segmentChanged:(id)sender{
UISegmentedControl *segmentedControl = sender;
switch (segmentedControl.selectedSegmentIndex) {
case 0:
self.currencyContainer.hidden = NO;
self.goldContainer.hidden = YES;
self.alarmContainer.hidden = YES;
break;
case 1:
self.currencyContainer.hidden = YES;
self.goldContainer.hidden = NO;
self.alarmContainer.hidden = YES;
break;
case 2:
self.currencyContainer.hidden = YES;
self.goldContainer.hidden = YES;
self.alarmContainer.hidden = NO;
break;
default:
break;
}
}
在任何服务器上,并发布结果。
可能,Android禁用了HttpCliente API(如果是Android 22 +)
答案 1 :(得分:0)
我会在php脚本中尝试回显响应,以查看数据是否实际来到服务器。然后你可以看到android是否发挥作用,你可以将问题隔离到android或php代码。 另外,我建议使用Retrofit或者排球来进行此类GET或POST请求。