我有發送參數代碼PHP和獲取引起JSON:發送參數,PHP和獲取引起JSON
private void details_location(int id){
HashMap<String, String> parameter = new HashMap<>();
parameter.put("id", id);
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url, new JSONObject(parameter), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
try {
JSONArray respon=(JSONArray)response.get("location");
for (int i = 0; i < respon.length(); i++) {
JSONObject person = (JSONObject) respon.get(i);
id_location= person.getString("id_location");
address= person.getString("address");
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
,我有文件PHP對於等待處理的查詢MySQL和具有輸出JSON:
<?php
require_once('db_connect.php');
if(isset($_GET['id'])){
$id=$_GET['id'];
$action="SELECT * FROM locaions WHERE id_location=\"$id\";";
$query=mysqli_query($db_connect, $action);
if (mysqli_num_rows($query) > 0)
{
$json['location']=array();
while($row=mysqli_fetch_assoc($query)){
$data=array();
$data["id_location"]=$row["id_location"];
$data["address"]=$row["address"];
array_push($json['location'], $data);
}
}
mysqli_close($db_connect);
echo json_encode($json);
}
else{
$action="SELECT * FROM locaions WHERE id_location=4;";
$query=mysqli_query($db_connect, $action);
if (mysqli_num_rows($query) > 0)
{
$json['location']=array();
while($row=mysqli_fetch_assoc($query)){
$data=array();
$data["id_location"]=$row["id_location"];
$data["address"]=$row["address"];
array_push($json['location'], $data);
}
}
mysqli_close($db_connect);
echo json_encode($json);
}
?>
當我運行該程序時,結果總是顯示位置id_location=4
(語句else)。爲什麼isset($_GET['id']) = false
?如何將參數發送到PHP並獲得JSON結果?
要回答「爲什麼isset($ _ GET ['id'])爲false」的問題,您應該打印您收到的值以及您嘗試訪問的URL,以便您更好地理解正在發生的事情。 –