Android Volley - Checking internet state
This is how I make Volley Request and handle response and errors, you don't need to add Async task for this, volley make request in backend
StringRequest strReq = new StringRequest(Request.Method.POST, "your_url", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// handle your response here
// if your response is a json then create json object and use it
try {
JSONObject jsonObject = new JSONObject(response);
// now you can get values from your jsonObject
}
catch (Exception e){}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
String message = null; // error message, show it in toast or dialog, whatever you want
if (volleyError instanceof NetworkError || volleyError instanceof AuthFailureError || volleyError instanceof NoConnectionError || volleyError instanceof TimeoutError) {
message = "Cannot connect to Internet";
} else if (volleyError instanceof ServerError) {
message = "The server could not be found. Please try again later";
} else if (volleyError instanceof ParseError) {
message = "Parsing error! Please try again later";
}
}
}) {
@Override
public byte[] getBody() throws AuthFailureError {
HashMap<String, String> params = new HashMap<>();
params.put("key","value"); // put your params here
return new JSONObject(params).toString().getBytes();
}
@Override
public String getBodyContentType() {
return "application/json";
}
};
// Adding String request to request queue
Volley.newRequestQueue(getApplicationContext()).add(strReq);
There is NoConnection Error that get thrown for the Request. Please catch the error in
@Override
public void onErrorResponse(VolleyError volleyError) {
String message = null;
if (volleyError instanceof NetworkError) {
message = "Cannot connect to Internet...Please check your connection!";
} else if (volleyError instanceof ServerError) {
message = "The server could not be found. Please try again after some time!!";
} else if (volleyError instanceof AuthFailureError) {
message = "Cannot connect to Internet...Please check your connection!";
} else if (volleyError instanceof ParseError) {
message = "Parsing error! Please try again after some time!!";
} else if (volleyError instanceof NoConnectionError) {
message = "Cannot connect to Internet...Please check your connection!";
} else if (volleyError instanceof TimeoutError) {
message = "Connection TimeOut! Please check your internet connection.";
}
}
I use below code for detecting which error is occurring:
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
Toast.makeText(getApplicationContext(), "Communication Error!", Toast.LENGTH_SHORT).show();
} else if (error instanceof AuthFailureError) {
Toast.makeText(getApplicationContext(), "Authentication Error!", Toast.LENGTH_SHORT).show();
} else if (error instanceof ServerError) {
Toast.makeText(getApplicationContext(), "Server Side Error!", Toast.LENGTH_SHORT).show();
} else if (error instanceof NetworkError) {
Toast.makeText(getApplicationContext(), "Network Error!", Toast.LENGTH_SHORT).show();
} else if (error instanceof ParseError) {
Toast.makeText(getApplicationContext(), "Parse Error!", Toast.LENGTH_SHORT).show();
}
}
});