Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google Volley无法从传递的参数中获取JSON值_Json_Android Volley_Http Get_Jsonobjectrequest - Fatal编程技术网

Google Volley无法从传递的参数中获取JSON值

Google Volley无法从传递的参数中获取JSON值,json,android-volley,http-get,jsonobjectrequest,Json,Android Volley,Http Get,Jsonobjectrequest,我试图传递一个参数,即userID,然后以JSON的形式获得响应 因此,如果userID=1,则响应为 [{“卡纳姆”:“本田”、“卡莫代尔”:“思域”}] 如果userID=5,则响应为 [{“卡纳姆”:“本田”、“卡莫代尔”:“思域”}、{“卡纳姆”:“大众”、“卡莫代尔”:“高尔夫”}、{“卡纳姆”:“福特”、“卡莫代尔”:“福克斯”}] 但是由于某些原因,参数没有被传递,如果它们被传递,那么我就无法在JSON 下面是我的代码: public void getComments(int us

我试图传递一个参数,即
userID
,然后以
JSON
的形式获得响应

因此,如果
userID=1
,则响应为
[{“卡纳姆”:“本田”、“卡莫代尔”:“思域”}]
如果
userID=5
,则响应为
[{“卡纳姆”:“本田”、“卡莫代尔”:“思域”}、{“卡纳姆”:“大众”、“卡莫代尔”:“高尔夫”}、{“卡纳姆”:“福特”、“卡莫代尔”:“福克斯”}]

但是由于某些原因,参数没有被传递,如果它们被传递,那么我就无法在
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);
}
public void getComments(int userID){
字符串passURL=“xxxxxxxxxxxxxxxxxxxxxxxxxxxx”;
JsonArrayRequest JsonArrayRequest=新JsonArrayRequest
(passURL,newresponse.Listener(){
@凌驾
公共void onResponse(JSONArray JSONArray){
试一试{
for(int i=0;i
我怀疑这与我在传递参数后试图从数组中获取响应有关。我的
php
Postman

中使用mcxiaoke库1.0.14及以上版本

JsonArrayRequest

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

JsonArrayRequest jsonArrReq = new JsonArrayRequest(Method.POST,
url, null,new Response.Listener<JSONArray>() 
{
......
//onResponse
......
}
JsonArrayRequest jsonArrReq=新的JsonArrayRequest(Method.POST,
url,null,新响应。侦听器()
{
......
//答复
......
}
getParams()

@覆盖
受保护的映射getParams(){
Map params=新的HashMap();
参数put(“用户”、“安卓”);
返回参数;
}

我不能为新的JsonArrayRequest提供超过3个参数
@LukazsPioetrszci:你应该在
https://github.com/mcxiaoke/android-volley
要想拥有更多的
JsonArrayRequest的构造函数
使用@BNK提到的截击库..您需要在请求中发送任何头吗?
@Override
protected Map<String, String> getParams() {
     Map<String, String> params = new HashMap<String, String>();
     params.put("user", "Android");
     return params;
}