如何在响应成功时获取volley库中的http状态代码。

时间:2021-01-08 00:13:20

I know its a repetitive question, but still. Can any one provide me a solution or workaround to get status code returned with the response when we make a network rest api call?

我知道这是一个重复的问题,但仍然存在。当我们进行网络休息api呼叫时,任何人都可以为我提供解决方案或解决方法来获取响应返回的状态代码吗?

Here provided a work around. But I didn't understood. Can any one explain me in a better way the solution.?

这提供了一个解决方案。但我没理解。任何人都可以用更好的方式解释我的解决方案。

The rest api returns many success status code like 201,204 and many server errors.

其余的api返回许多成功状态代码,如201,204和许多服务器错误。

I want to check status code before proceeding and have to make decision accordingly.

我想在继续之前检查状态代码,并且必须做出相应的决定。

This is my existing code.

这是我现有的代码。

on a button click

点击按钮

RequestQueue queue;
queue = Volley.newRequestQueue(this);
final String[] token = new String[1];
String status;
String url = "http://myapiurl";
JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.GET, url,
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                // want to get status code here
                token[0] = response.getString("message");
                Toast.makeText(getApplicationContext(),token[0],Toast.LENGTH_SHORT).show();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // error
            Log.d("Error.Response", String.valueOf(error));
        }
    }
);
queue.add(postRequest);

1 个解决方案

#1


EDIT: previously didn't answer the question at all.

编辑:以前根本没有回答这个问题。

It seems nontrivial to do this while using the clean Listener interfaces, but it's also possible to implement this as a custom request (the examples specifically show parsing JSON). You can look at the JsonObjectRequest and JsonRequest sources for inspiration.

在使用干净的Listener接口时执行此操作似乎并不重要,但也可以将其实现为自定义请求(示例专门显示解析JSON)。您可以查看JsonObjectRequest和JsonRequest源代码的灵感。

You should end up with something like this (heavily inspired by JsonObjectRequest):

你应该得到这样的东西(受JsonObjectRequest的启发):

Request<JSONObject> request = new Request<JSONObject>() {
  @Override
  protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
    try {
      String jsonString = new String(
        response.data,
        HttpHeaderParser.parseCharset(response.headers));
      JSONObject obj = new JSONObject(jsonString);
      // can access response.statusCode here
      // ...
      return Response.success(obj, HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
      return Response.error(new ParseError(e));
    } catch (JSONException je) {
      return Response.error(new ParseError(je));
    }
  }
};

#1


EDIT: previously didn't answer the question at all.

编辑:以前根本没有回答这个问题。

It seems nontrivial to do this while using the clean Listener interfaces, but it's also possible to implement this as a custom request (the examples specifically show parsing JSON). You can look at the JsonObjectRequest and JsonRequest sources for inspiration.

在使用干净的Listener接口时执行此操作似乎并不重要,但也可以将其实现为自定义请求(示例专门显示解析JSON)。您可以查看JsonObjectRequest和JsonRequest源代码的灵感。

You should end up with something like this (heavily inspired by JsonObjectRequest):

你应该得到这样的东西(受JsonObjectRequest的启发):

Request<JSONObject> request = new Request<JSONObject>() {
  @Override
  protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
    try {
      String jsonString = new String(
        response.data,
        HttpHeaderParser.parseCharset(response.headers));
      JSONObject obj = new JSONObject(jsonString);
      // can access response.statusCode here
      // ...
      return Response.success(obj, HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
      return Response.error(new ParseError(e));
    } catch (JSONException je) {
      return Response.error(new ParseError(je));
    }
  }
};