在我的安卓系统中:
String cat_id = getIntent().getExtras().getString("category_id");
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("cat_id", cat_id));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://www.---.com/items.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection" + e.toString());
}
在我的PHP中:
// editing out DB connections which have been verified to work
<?php
$sql=mysql_query("SELECT * FROM items WHERE cat_id = '".$_REQUEST['cat_id']."'");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>
所以我的问题是它什么也不返回(在JSON结果部分——Toast说“没有找到任何项”)。我的代码是否正确,我将参数从Android传递到PHP的方式?
最佳答案
这条线不应该
$sql=mysql_query("SELECT * FROM items WHERE cat_id = '".$_REQUEST['cat_id']."'");
是
$sql=mysql_query("SELECT * FROM items WHERE cat_id = '".$_POST['cat_id']."'");
即$请求$邮寄?
我建议使用Firefox的Firebug插件,并跟踪发送到服务器的确切表单。