发送整数数组作为凌空的PARAMS的值返回400代码

问题描述:

我试图发送以下JSON:发送整数数组作为凌空的PARAMS的值返回400代码

{"communities" : [381, 382, 383, 384, 385]} 

使用,当我试图发送一个哈希表与此代码我没有problemst String作为键和值,但是在这种情况下,当值应为整数数组服务器不承认它是这样和我返回代码400:

JSONObject dataToPost = new JSONObject(); 

JSONArray jsa = new JSONArray(); 
jsa.put(381); 
jsa.put(382); 
jsa.put(383); 
jsa.put(384); 
jsa.put(385); 
jsa.put(386); 

try { 
    dataToPost.put("communities", jsa); 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 

final JSONObject dataFinal = dataToPost; 

Log.i("fdsfs", dataFinal.toString()); 

StringRequest postRequest = new StringRequest(Request.Method.POST, GlobalValues.registerSetCommunities, 

     new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 

       manageSuccessResponse(response); 
      } 
     }, 
     new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 

       manageErrorResponse(error); 
      } 
     } 
) { 

    @Override 
    public byte[] getBody() { 
     try { 
      return dataFinal.toString().getBytes("utf-8"); 
     } catch (UnsupportedEncodingException e) { 
      return null; 
     } 
    } 

    @Override 
    public Map<String, String> getHeaders() throws AuthFailureError { 
     Map<String, String> headers = new HashMap<String, String>(); 
     headers.put("Content-Type", "application/x-www-form-urlencoded"); 
     headers.put("Authorization", "Bearer " + Credentials.getAuthToken(mContext)); 
     return headers; 
    } 
}; 

postRequest.setRetryPolicy(new DefaultRetryPolicy(
     GlobalValues.request_timeout, 
     DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
     DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 

Volley.newRequestQueue(mContext).add(postRequest); 

的另一种方式,我试图用JsonObjectRequest。下面是代码:

JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, GlobalValues.registerSetCommunities, dataFinal, new Response.Listener<JSONObject>() { 
    @Override 
    public void onResponse(JSONObject response) { 
     Log.i("SENDMOVE", response.toString()); 
    } 
}, new Response.ErrorListener() { 
    @Override 
    public void onErrorResponse(VolleyError error) { 
     manageErrorResponse(error); 
    } 
}) { 
    @Override 
    public Map<String, String> getHeaders() throws AuthFailureError { 
     Map<String, String> headers = new HashMap<String, String>(); 
     headers.put("cache-control", "no-cache"); 
     headers.put("Content-Type", "application/json"); 
     headers.put("Authorization", "Bearer " + Credentials.getAuthToken(mContext)); 
     return headers; 
    } 
}; 

request.setRetryPolicy(new DefaultRetryPolicy(
     GlobalValues.request_timeout, 
     DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
     DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 

Volley.newRequestQueue(mContext).add(request); 
+0

如果请求成功收到,请尝试在服务器端打印值。我们无法确定哪些代码会导致该错误。 – Enzokie

使用第二种方法,在去除

headers.put("Content-Type", "application/json"); 

的伎俩。