Get HTTP status code for successful requests with Volley

前端 未结 4 1330
佛祖请我去吃肉
佛祖请我去吃肉 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:24

    I will make the response from VinceStyling more complete. I'll tell you what I do.

    Once you override this method, save the statusCode in your class.

            @Override
            protected Response parseNetworkResponse(NetworkResponse response) {
                statusCode=response.statusCode;
                return super.parseNetworkResponse(response);
            }
    

    After that you should compare it with HttpURLConnection constants to act accordingly. For example:

                int statusCode=webService.getStatusCode();
                switch (statusCode){
                    case HttpURLConnection.HTTP_OK:
                        //do stuff
                        break;
                    case HttpURLConnection.HTTP_NOT_FOUND:
                        //do stuff
                        break;
                    case HttpURLConnection.HTTP_INTERNAL_ERROR:
                        //do stuff
                        break;
                }
    

提交回复
热议问题