使用截击在身体中传递带有传递列表的请求

时间:2019-04-10 19:26:08

标签: android android-volley

我已经创建了spring boot api,现在我将它与android app集成在发布请求中,我在通过邮递员进行测试时通过了一个列表,它运行良好,但是当我从android发送列表时却不起作用。请告诉我如何使用凌空库 this is image of postman

在json正文中发送列表

这是我的代码

 RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
JSONObject jsonBody = new JSONObject();
List<Integer> ids=new ArrayList<>();
ids.add(11);
ids.add(12);
jsonBody.put("ids",ids);
final String mRequestBody = jsonBody.toString();

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_ADD_SKILLS, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {


        Log.i("LOG_RESPONSE", response);
    }
}, error -> Log.e("LOG_VOLLEY", error.toString())) {
    @Override
    public String getBodyContentType() {
        return "application/json";
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        try {
            return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
        } catch (UnsupportedEncodingException uee) {
            VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
            return null;
        }
    }

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        String responseString = "";
        if (response != null) {

            responseString = String.valueOf(response.statusCode);

        }
        return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
    }
};

requestQueue.add(stringRequest);

1 个答案:

答案 0 :(得分:0)

因为您传递了错误的json格式来发布如下所示的API。

{
  "ids": "[11, 12]"
}

但是在邮递员中,您正在对象中传递jsonarray id。

{ 
  "ids":[11,12]
}

因此像这样修改您的代码

JSONObject jsonBody = new JSONObject();

JSONArray jArrIds=new JSONArray();
jArrIds.put(11);
jArrIds.put(12);
jsonBody.put("ids",jArrIds);