【问题标题】:How to send JSONArray as params and recieve JSONObject as response如何发送 JSONArray 作为参数并接收 JSONObject 作为响应
【发布时间】:2021-10-31 08:05:25
【问题描述】:

我有一个将 JSONArray 作为参数的 API,它提供一个 JSONObject 作为响应。 API 工作正常,但报错

    com.android.volley.ParseError: org.json.JSONException: End of input at character 0 of 
        at com.android.volley.toolbox.JsonArrayRequest.parseNetworkResponse

当我收到回复时。

JsonArrayRequest volleyRequest= new JsonArrayRequest(Request.Method.POST, url,
                params, res -> {
            try {
                Log.d("TAG", "PostApiMethod: "+res);
            } catch (JSONException e) {
                Log.e("TAG", "PostApiMethod: ", e);
            }

        }, error -> {

            Log.e(TAG, "PostApiMethod: ", error);
            }

        }) {
            @Override
            public String getBodyContentType() {
                return "application/x-www-form-urlencoded; charset=UTF-8";
            }
        };

        VolleySingleton.getInstance(context).addToRequestQueue(volleyRequest);

在使用 Volley 时有什么方法可以做到这一点。

【问题讨论】:

  • 你能编辑问题集 JSONArray 结构吗

标签: java android android-volley


【解决方案1】:

您可以使用 StringRequest 并将 JSONArray 转换为 String 作为参数,并将结果作为 String 并将字符串映射到 JSONObject 就像这段代码一样

JSONArray jsonBody=new JSONArray();
 try {
 /// add jSONObject inside JSONArray as example 
JSONObject jsonObject=new JSONObject();
  jsonObject.put("id",1);
  jsonObject.put("name","test");
  jsonBody.put(jsonObject);
} catch (Exception e) {
  e.printStackTrace();
}
final String requestBody = jsonBody.toString();
StringRequest request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
     @Override
     public void onResponse(String response) {
        textView.setText(response.toString());
        try {
           JSONObject object=new JSONObject(response);
           // do every thing using json object
        } catch (JSONException e) {
           e.printStackTrace();
        }
     }
  }, new Response.ErrorListener() {
     @Override
     public void onErrorResponse(VolleyError error) {
        Log.d("error",error.toString());
     }
  }){
    @Override
    public String getBodyContentType() {
        return "application/json; charset=utf-8";
    }

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

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        String responseString = "";
        if (response != null) {
            responseString = String.valueOf(response.statusCode);
            // can get more details such as response.headers
        }
        return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
    }
};

我建议使用改造库而不是 Volley

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-26
    • 1970-01-01
    • 2021-03-17
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多