0

我正在尝试传递一个参数,userID然后以JSON.

因此,如果userID=1那么响应将是 [{"carname":"Honda","carmodel":"Civic"}] ,如果userID=5那么响应将是 [{"carname":"Honda","carmodel":"Civic"},{"carname":"VW","carmodel":"Golf"},{"carname":"Ford","carmodel":"Focus"}]

但由于某种原因,参数没有被传递,如果是,那么我无法检索值JSON

这是我下面的代码:

public void getComments(int userID){
    String passURL = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest
            (passURL, new Response.Listener<JSONArray>(){

                @Override
                public void onResponse(JSONArray jsonArray) {
                    try {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);

                            String carName = jsonObject.getString("carname");
                            String carModel = jsonObject.getString("carmodel");
                            UserStore userStore = new UserStore(carName, carModel);
                            list.add(userStore);
                            adapter.notifyDataSetChanged();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError volleyError) {

                }

            }) {
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("userID", Integer.toString(userID));
            return params;
        }
    };
    requestQueue.add(jsonArrayRequest);
}

我怀疑这与我在传递参数后试图从数组中获取响应这一事实有关。我的php作品在Postman

4

1 回答 1

0

使用mcxiaoke库1.0.14及以上版本

JsonArrayRequest

您需要在 JsonArrayRequest 中将 Method 设置为 Post,如下所示

JsonArrayRequest jsonArrReq = new JsonArrayRequest(Method.POST,
url, null,new Response.Listener<JSONArray>() 
{
......
//onResponse
......
}

获取参数()

@Override
protected Map<String, String> getParams() {
     Map<String, String> params = new HashMap<String, String>();
     params.put("user", "Android");
     return params;
}
于 2016-03-14T16:57:19.733 回答