从截击错误响应中获取响应代码

Getting response code from volley error response

我们如何获取客户端错误响应的响应代码和消息?这里 error.toString() returns: com.android.volley.ServerError error.getMEssage(): null error.getCause(): null parseNetworkResponse: 永远不会进入那个函数。当我添加 getParams() 方法时,它可能 return ClientError 但响应代码等仍然不起作用!!!!!!!我在过去 24 小时内一直在尝试这个。任何地方都没有答案。

private void getStudentInfo(String number){
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
final String url = organizationRoot+"/identity/detailed/" +number ;
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        txtTagContent.setText(response);
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

        txtTagContent.setText("ERROR " + error.toString() + error.getMessage() + error.getCause());
    }
}){

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        return requestHeaders;
    }

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
            txtTagContent.setText("WORKED ");
            return Response.success( "", HttpHeaderParser.parseCacheHeaders(response));
    }
};
requestQueue.add(stringRequest);

}

你能试试吗:

int statusCode = error.networkResponse.statusCode;
Log.e(TAG, String.valueOf(statusCode);

也许你可以试试这个

new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    String message = null;
                    if (error instanceof NetworkError) {
                        message = "Cannot connect to Internet...Please check your connection!";
                    } else if (error instanceof ServerError) {
                        message = "The server could not be found. Please try again after some time!!";
                    } else if (error instanceof AuthFailureError) {
                        message = "Cannot connect to Internet...Please check your connection!";
                    } else if (error instanceof ParseError) {
                        message = "Parsing error! Please try again after some time!!";
                    } else if (error instanceof NoConnectionError) {
                        message = "Cannot connect to Internet...Please check your connection!";
                    } else if (error instanceof TimeoutError) {
                        message = "Connection TimeOut! Please check your internet connection.";
                    }
                    Toast.makeText(LoginActivity.this, message, Toast.LENGTH_SHORT).show();
                }
            }