如何获取 Volley POST 响应正文?

我正在使用类似的功能来创建 GET 和 POST api 请求,但是 GET 函数将 textview 设置为响应正文,我不知道如何读取状态代码,POST 函数将 textview 设置为响应代码,我不知道如何读取响应身体。你能帮我吗?


我也尝试登录 parseNetworkResponse() response.data.toString() 但这不是 api 返回的数据。也许我需要以某种方式对其进行编码?


public void createGet(Context context, String url) {

      final TextView apiResultTextview = (TextView) ((Activity) 

  context).findViewById(R.id.api_result_textview);

      // Instantiate the RequestQueue.

         RequestQueue queue = Volley.newRequestQueue(context);


    // Request a string response from the provided URL.

    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,

            new Response.Listener<String>() {

                @Override

                public void onResponse(String response) {

                    // Display the first 500 characters of the response string.

                    apiResultTextview.setText("Response is: " + response.substring(0, 50));

                }

            }, new Response.ErrorListener() {

        @Override

        public void onErrorResponse(VolleyError error) {

            apiResultTextview.setText(error.toString());

        }

    });


    // Add the request to the RequestQueue.

    queue.add(stringRequest);

}


public void createPost(Context context, String url, JSONObject body) {

    final TextView apiResultTextview = (TextView) ((Activity) context).findViewById(R.id.api_result_textview);

    RequestQueue requestQueue = Volley.newRequestQueue(context);

    final String mRequestBody = body.toString();


    StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {

        @Override

        public void onResponse(String response) {

            Log.i("LOG_RESPONSE", response);

        }

    }, new Response.ErrorListener() {

        @Override

        public void onErrorResponse(VolleyError error) {

            Log.e("LOG_RESPONSE", error.toString());

        }

    }) {

 


喵喵时光机
浏览 107回答 2
2回答

白衣染霜花

我通过更改下面的代码调用方法来解决它。问题可能出在 JsonObjectRequest 与 StringRequest 中。这就是为什么我只得到响应代码字符串的原因。public void createCall(int type, String url, JSONObject data, final int callback) {&nbsp; &nbsp; JsonObjectRequest jsonRequest = new JsonObjectRequest(type, url,data,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Response.Listener<JSONObject>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onResponse(JSONObject response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("Response", response.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callback(response, callback);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("API callback error", e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Response.ErrorListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onErrorResponse(VolleyError error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("Error response", error.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; );&nbsp; &nbsp; queue.add(jsonRequest);}

冉冉说

在你的 onResponse() 方法中添加这段代码&nbsp;JSONObject jsonObj = new JSONObject(response);&nbsp;String response_value = jsonObj.getString("response");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java