2017-01-26 40 views
1

我试图与一个数组作为我的参数凌空POST请求,例如我要发布数组作为排球POST请求参数

{"types":[1,2,3]} 

什么我现在已经是一个字符串

{"types":"[1,2,3]"} 

这是我做了我的截击要求:

JSONArray jsonArray = new JSONArray(); 
JSONObject jsonObject = new JSONObject(); 

    try{ 
     jsonObject.put("types", list); 
     jsonArray.put(jsonObject); 
     System.out.println(jsonObject); 
    }catch(Exception e){ 

    } 

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, 
      new Response.Listener<JSONObject>(){ 
       @Override 
       public void onResponse(JSONObject response) { 
        Log.e("Response", response.toString()); 
       } 
      }, 
      new Response.ErrorListener(){ 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Log.e("Error.Response", error.toString()); 
        String json = null; 
        NetworkResponse response = error.networkResponse; 
        if(response != null && response.data != null){ 
         switch(response.statusCode){ 
          case 400: 

           json = new String(response.data); 
           System.out.println(json); 
           break; 
         } 
        } 
       } 
      }) 
    { 
     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      HashMap<String, String> headers = new HashMap<>(); 
      headers.put("Authorization", Token); 
      return headers; 
     } 
    }; 

    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(jsonObjectRequest); 

名单被声明为数组列表:

List<Integer> list = new ArrayList<Integer>(); 

我假设jsonObject.put(“类型”,列表)将我的数组变成一个列表,我应该如何解决这个问题?

+0

您需要从您的整数列表创建'JSONArray'并添加到'jsonObject' – akash93

回答

1

而不是List,请尝试JSONArray

JSONArray types=new JSONArray(); 
types.put(1); 
types.put(2); 
types.put(3); 
jsonObject.put("types", types); 
jsonArray.put(jsonObject); 

这应该可以解决您的问题

+0

很抱歉这么晚才回复,这正是问题谢谢! – JerryKo

+0

@JerryKo我需要帮助 –

+0

@ AdityaVyas-Lakhan我能帮你吗? – JerryKo