【发布时间】:2016-03-07 08:45:54
【问题描述】:
我知道如何使用 Volley GET 方法进行登录,但如果我需要指定特定的“id”怎么办?我使用intent.putExtra 将列表项的“id”从远程数据库传递到另一个活动。在下一个活动中,我将如何利用 Volleys 的 GET 方法来解析和显示另一个使用“id”传递的表中的数据?
在浏览器中,我可以使用以下命令获取带有指定 url 和参数的响应:
http://localhost/demoapp/fetch.php?pid=2
但我不知道如何将该 url 传递给 Volley 请求并在列表视图中显示响应。像这样的:
"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 的不同表中的 cmets 列表。
【问题讨论】:
-
您在使用 GET 方法时遇到什么问题?您似乎遵循了正确的程序。
-
@JyotmanSingh 试图弄清楚如何在 Android 应用程序中检索该 json 响应。请看我的下一个编辑。
-
您提到您知道如何使用 Volley GET 方法。以与您用于登录的方式类似的方式使用它。使用
JsonObjectRequest获取json数据,然后解析并显示在你的应用中。
标签: php android android-volley