我知道如何使用Volley GET方法登录的目的,但如果我需要指定一个特定的“ID”?我使用intent.putExtra将远程数据库中列表项的“id”传递给另一个活动。在下一个活动中,我将如何利用Volleys的GET方法使用通过“id”传递的另一个表来解析和显示数据?如何使用指定参数的Android Volley GET方法?
在浏览器中,我可以使用以下方法来获取与指定的URL和参数的响应:
http://localhost/demoapp/fetch.php?pid=2
但我不知道如何该网址传递给凌空请求,并显示在响应一个listview。例如:
"http://localhost/demoapp/fetch.php?pid=" + pid
其中“pid”将是相应id的传递字符串。
编辑:PHP代码
<?php
include_once("config.php");
$query=mysqli_query($con,"SELECT * FROM comments WHERE pid=".$_GET['pid']);
$array;
while($result=mysqli_fetch_assoc($query)){
$array[]=$result;
}
echo json_encode($array);
?>
编辑2:Java代码 主要活动
JsonArrayRequest request = new JsonArrayRequest(url+url_file,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString()); try {
for(int i=0;i<response.length();i++){
String pid=response.getJSONObject(i).getString("pid");
String name=response.getJSONObject(i).getString("product_name");
String img;
String thumb = response.getJSONObject(i).getString("product_thumb");
String detail = response.getJSONObject(i).getString("product_detail");
String rating = response.getJSONObject(i).getString("product_rating");
img = response.getJSONObject(i).getString("product_pic");
rowdata.add(new ProductRowData(pid,name,img,thumb,detail,rating));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
adapter=new ProductsAdapter(MainActivity.this, rowdata);
list.setAdapter(adapter);
dialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error: " + error.getMessage());
dialog.dismiss();
}
});
VolleyController.getInstance().addToRequestQueue(request, tag_json_arry);
当单击列表项,数据的其余部分被传递到细节使用intent.putExtra的活动。然后我试图做的是,在点击FAB时,应该显示来自不同表格和该产品ID的评论列表。
什么你用GET方法面临的问题是什么?你似乎正在遵循正确的程序。 –
@JyotmanSingh试图找出如何在Android应用程序中检索该json响应。请参阅我的下一个编辑。 –
你提到你知道如何使用Volley GET方法。以类似于您用于登录的方式使用它。使用'JsonObjectRequest'来获取json数据,然后解析它并在你的应用程序中显示它。 –