将参数发送到php并以JSON格式获取结果
我有将参数发送到php并以JSON获取结果的代码:将参数发送到php并以JSON格式获取结果,php,android,android-studio,android-volley,Php,Android,Android Studio,Android Volley,我有将参数发送到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,
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
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);
}
?>
private void details\u位置(int id){
HashMap参数=新的HashMap();
参数put(“id”,id);
JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(Method.GET、url、新的JSONObject(参数)、新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
Log.d(TAG,response.toString());
试一试{
JSONArray respon=(JSONArray)response.get(“位置”);
对于(int i=0;i
我有处理查询Mysql的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
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\u location=4
(语句else)显示位置。为什么isset($\u GET['id'])=false
?如何将参数发送到PHP并在JSON中获得结果?这是将参数发送到ur PHP服务器的代码
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
String parameter1 = (String) params[0];
String parameter2 = (String) params[1];
String link = "Mention ur url link here/parameter1+parameter1 +parameter2+parameter2+";
URL url = new URL(link);
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(link));
HttpResponse response = client.execute(request);
BufferedReader in = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
while ((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
return sb.toString();
}
catch (Exception e) {
return new String("Exception: " + e.getMessage());
}
}
在执行后,php应该返回json格式的数据,您可以在执行后处理这些数据要回答“为什么isset($\u GET['id'])为false”的问题,您应该打印您收到的值以及您试图访问的URL,这样您就可以更好地了解发生了什么。