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
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.
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.
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.
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); }
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;
}
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With