【问题标题】:How to use Android Volley GET method with specified parameter?如何使用带有指定参数的 Android Volley GET 方法?
【发布时间】: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


【解决方案1】:

如果你想使用 GET,你可以使用 url 参数创建请求:

String uri = String.format("http://localhost/demoapp/fetch.php?pid=%1$s", pid);

StringRequest myReq = new StringRequest(Method.GET,
                                    uri,
                                    createMyReqSuccessListener(),
                                    createMyReqErrorListener());

或者更好,你可以使用 POST:

StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://localhost/demoapp/fetch.php", new Response.Listener<String>()
{
    @Override
    public void onResponse(String s)
    {
    }
}, new Response.ErrorListener()
{
    @Override
    public void onErrorResponse(VolleyError volleyError)
    {
    }
})
{
    @Override
    protected Map<String, String> getParams()
    {
        Map<String, String> params = new HashMap<String, String>();
        params.put("id", pid);
        return params;
    }
};

取自:Volley - POST/GET parameters

【讨论】:

  • 如果我使用 POST 方法,我是否必须将 PHP 代码更改为 .$_POST['pid'] 而不是 .$_GET['pid']
  • 是的,你必须改变你的后端。
  • 如果我更改 PHP 中的方法并尝试使用带有添加参数的相同 url,我会在 id 上得到一个未定义的索引
  • 如果您使用的是POST,则无需手动添加参数。我将对其进行编辑。
  • 我已尝试按照您的回答运行我的应用程序,但出现错误。我可以使用您的帮助,而不是发布具有相同问题的另一个问题。如果你有空,请告诉我。我需要尽快解决这个问题。谢谢。
【解决方案2】:
        @Override
        protected Map<String, String> getParams() {
            HashMap<String,String> params = new HashMap<>();
            params.put("deviseType","1");
            params.put("langId","1");
            params.put("userType","2");
            return params;
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    • 1970-01-01
    • 2013-05-13
    相关资源
    最近更新 更多