Get HTTP status code for successful requests with Volley

前端 未结 4 1319
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-28 16:03

I\'m retrieving the content of a invalid web address with volley, i.e. http://www.gigd32fdsu.com: This is my test code:

// Instantiate the Reque         


        
4条回答
  •  旧时难觅i
    2020-12-28 16:38

    @VinceStyling 's answer is ok, or you can extend Request class and do what you wanna do. For example,

        ServerStatusRequestObject extends Request {
    
        private final Response.Listener mListener;
        private String mBody = "";
        private String mContentType;
    
        public ServerStatusRequestObject(int method,
                                         String url,
                                         Response.Listener listener,
                                         Response.ErrorListener errorListener) {
    
            super(method, url, errorListener);
            mListener = listener;
            mContentType = "application/json";
    
            if (method == Method.POST) {
                RetryPolicy policy = new DefaultRetryPolicy(5000, 0, 5);
                setRetryPolicy(policy);
            }
        }
    
        @Override
        protected Response parseNetworkResponse(NetworkResponse response) {
            return Response.success(response.statusCode, HttpHeaderParser.parseCacheHeaders(response));
        }
    
        @Override
        protected void deliverResponse(Object response) {
            if (mListener != null) {
                mListener.onResponse(response);
            }
        }
    
        @Override
        public byte[] getBody() throws AuthFailureError {
            return mBody.getBytes();
        }
    
        @Override
        public String getBodyContentType() {
            return mContentType;
        }
    
        @Override
        public int compareTo(Object another) {
            return 0;
        }
    

    then in your response handler, you can still receive the whole messages from server. Try it.

提交回复
热议问题