Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get HTTP status code for successful requests with Volley

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 RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
final String url = "http://www.gigd32fdsu.com";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, 
new Response.Listener() {
    @Override
    public void onResponse(Object response) {
        // Display the first 500 characters of the response string.
        mTextView.setText("Response is: " + response.toString().substring(0, 500));
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        mTextView.setText("That didn't work! " + error.networkResponse.statusCode);
    }
});
// Add the request to the RequestQueue.
queue.add(stringRequest);

When I run this code I receive the callback onResponse(String) with an error page from my ISP. How can I read the HTTP status code in order to detect that the web displaying is not correct?

Thanks

like image 669
Addev Avatar asked Sep 24 '14 11:09

Addev


People also ask

How do I get a volley status code?

Just override the parseNetworkResponse method then take the statusCode value. Show activity on this post. then in your response handler, you can still receive the whole messages from server. Try it.

What will be status code you get when your request get success?

The 200 OK status code means that the request was successful, but the meaning of success depends on the request method used: GET: The requested resource has been fetched and transmitted to the message body.

Which HTTP status code is associated with a success response of get method?

200 OK: The HTTP 200 OK response meaning is that the request made by the client has been successful, but the meaning of the success depending on the four type of request made by the clients. The GET method fetch and transmitted the resources in the message body.


Video Answer


3 Answers

Simple solution is to override parseNetworkResponse in makeStringReq(), no need for another class:

private void makeStringReq() {     showProgressDialog();      StringRequest strReq = new StringRequest(Method.GET,             Const.URL_STRING_REQ,             new Response.Listener<String>() {                 @Override                 public void onResponse(String response) {                     Log.d(TAG, response.toString());                     msgResponse.setText(response.toString());                     hideProgressDialog();                  }             },             new Response.ErrorListener() {                 @Override                 public void onErrorResponse(VolleyError error) {                     VolleyLog.d(TAG, "Error: " + error.getMessage());                     hideProgressDialog();                 }             }) {          @Override         protected Response<String> parseNetworkResponse(NetworkResponse response) {             int mStatusCode = response.statusCode;             return super.parseNetworkResponse(response);         }     };      // Adding request to request queue     AppController.getInstance().addToRequestQueue(strReq, tag_string_req);  } 
like image 180
5er Avatar answered Sep 23 '22 19:09

5er


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<String> 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;
            }
like image 31
Dantalian Avatar answered Sep 21 '22 19:09

Dantalian


Just override the parseNetworkResponse method then take the statusCode value.

public class StrImplRequest extends StringRequest {
    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        // take the statusCode here.
        response.statusCode;
        return super.parseNetworkResponse(response);
    }
}
like image 33
VinceStyling Avatar answered Sep 23 '22 19:09

VinceStyling