我有一个应用程序,它通过 php 脚本从 phpmyadmin 读取 Json 数据并显示在列表 Activity 中。单击商店名称后,+1 将添加到该商店的投票计数中,并且应该发送回 php 服务器以将新的投票计数存储在 phpmyadmin 中。选择后,我检查了 db vote count 值,它没有更新。虽然我在 logcat 中得到 HTTP/1.1 200 ok,但我认为数据没有被正确传递或接收。有人可以帮忙吗,我被困住了,没有方向。
安卓代码:
public void writeJSON() {
String convertedID;
String convertedVote;
//convert int to string value to passed
convertedID = new Integer(selectedID).toString();
convertedVote = new Integer(selectedVote).toString();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/kcstores.php");
try {
//writes the output to be stored in creolefashions.com/test2.php
ArrayList <NameValuePair> nvps = new ArrayList <NameValuePair>(2);
nvps.add(new BasicNameValuePair("storeUpdate", "update"));
nvps.add(new BasicNameValuePair("storeID", convertedID));
nvps.add(new BasicNameValuePair("storeVote", convertedVote));
httppost.setEntity(new UrlEncodedFormEntity(nvps));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
Log.i("writeJSON", response.getStatusLine().toString());
} catch(Exception e) {
Log.e("log_tag", "Error in http connection"+e.toString());
}
}
PHP 代码:
<?php
$link = mysql_connect("localhost", "root", "") or die (mysql_error());
mysql_select_db("king_cake_stores")or die (mysql_error());
$query = "SELECT * FROM storeInfo";
$result = mysql_query($query);
$getUpdate = "noupdate";
if (isset($_POST['storeUpdate'])) {
echo "receiving data from app";
$getUpdate = $_POST['storeUpdate'];
$getStoreID = $_POST['storeID'];
$getStoreVote = $_POST['storeVote'];
}
// If command == getStoreID, it updates the table storeVote value
// with the android storeVote value based upon correct storeID
if ($getUpdate == "update") {
mysql_select_db("UPDATE storeInfo SET storeVote = $getStoreVote
WHERE storeID == $getStoreID");
} else {
// stores the data in an array to be sent to android application
while ($line = mysql_fetch_assoc($result)) $output[]=$line;
print(json_encode($output));
}
mysql_close($link);
?>
最佳答案
我建议您从服务器端开始调试,然后开始逆向工作。
首先,开始记录来自您的 HttpResponse 的响应文本。
在您的 php 文件中回显 mysql 查询文本,并确保它看起来像您期望的那样。
如果看起来正确,请检查您的数据库结构。
如果没有,请尝试执行 var_dump($_POST)
,并检查您的参数是否已正确发送。
如果您完成这些步骤,您应该对问题出在哪里有更好的了解。
关于php - 数据未正确传递到 php 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10508060/