这是我想在Android中使用HttpPost方法的php代码。我想检查与post方法一起发送的json对象是否与数据库中存在的数据匹配
if(!isset($_POST['data'])) {
echo "FAIL1";
return 'false';
}
$jsonObject = $_POST['data'];
$data = json_decode($jsonObject, true);
if(!is_array($data)) {
print_r($data);
exit;
echo "FAIL2";
return 'false';
}
$UNIQUE_ID = $data['UNIQUE_ID'];
$BAR_CODE = $data['BARCODE'];
$BATCH_CODE = $data['BATCHCODE'];
if(!isset($UNIQUE_ID) || trim($UNIQUE_ID) == '' || !isset($BAR_CODE) || trim($BAR_CODE) == '' || !isset($BATCH_CODE) || trim($BATCH_CODE) == '') {
echo "FAIL3";
return 'false';
}
$link = mysql_connect($db_server,$db_username,$db_pasword) or die("Can not connect to server $db_server.");
mysql_select_db("drugverifier",$link);
$query = "SELECT * FROM drugverifier where unique_id = $UNIQUE_ID AND bar_code = $BAR_CODE AND batch_code = $BATCH_CODE";
echo " TEST4";
$result = mysql_query($query,$link) or die("Can not able to run the query ' $query ' on server $db_server.");
echo "TEST5";
if(mysql_num_rows($result) > 0){
echo "SUCCESS6";
echo 'true';
}
else {
echo "FAILURE7";
echo 'false';
}我的android代码是:
JSONObject j = new JSONObject();
try {
j.put("BARCODE", "8904017500190");
j.put("BATCHCODE", "220");
j.put("UNIQUE_ID", "9876");
} catch (JSONException e) {
Log.v("@@@@@JSONException", e.getMessage());
}
String url = "http://www.smartcloudinfotech.com/SmartDrugVerifier/verifyBarCode.php";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
List nameValuePair = new ArrayList(1);
nameValuePair.add(new BasicNameValuePair("data",j.toString()));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
response = httpclient.execute(httpPost);
Log.v("RESPONSE",""+response.toString());
Toast.makeText(this, "RESPONSE in doPOST()"+response.toString(), Toast.LENGTH_SHORT).show();
} catch (UnsupportedEncodingException e) {
Log.v("@@@@@@@@@@@@@@UnsupportedEncodingException", e.getMessage());
} catch (ClientProtocolException e) {
Log.v("@@@@@@@@@ClientProtocolException",e.getMessage());
} catch (IOException e) {
Log.v("@@@@@@@@@@IOException116", e.getMessage());
}
String temp = EntityUtils.toString(response.getEntity());当我运行这个程序时,变量temp的值不包含任何值。
请帮我解决我要出错的地方。