How to stop volley Request after 1 min - android

In my application, I made a volley request and it is working fine, but when the response does not reach the user, the loader keeps loading and it doesn't stop. What should I do? How do I stop volley requests after 1 min if the response is nothing?

you need to implement the ErrorListener and setRetryPolicy method and dismiss the progress in onErrorResponse method
JsonObjectRequest myRequest = new JsonObjectRequest(Method.GET,
url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
progressdialog.dismiss()
Log.d(TAG, "V_Error: " + error.getMessage());
/*if (volleyError.getClass().equals(TimeoutError.class)) {
// Show timeout error message
}*/
}
});
myRequest.setRetryPolicy(new DefaultRetryPolicy(
7000,//Socket time out in milies
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

Related

Getting com.android.volley.ClientError in my android API calling using volley

I am a new guy in android and trying to make a rest api call using volley.
I am getting "com.android.volley.ClientError" in my code. Can anyone help me to solve this please.
private void LoginByNet(String uID, String pSD){
String URL = "http://myipaddress:65017/api/values";
RequestQueue rq = Volley.newRequestQueue(this);
JsonObjectRequest jq = new JsonObjectRequest(
Request.Method.GET,
URL,
null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(getApplicationContext(), "Success:"+response.toString(), Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"Error:"+ error.toString(), Toast.LENGTH_SHORT).show();
userName.setText(error.toString());
}
}
);
rq.add(jq);
}
This means that the server returned a 4xx error code.
[https://github.com/google/volley/blob/d1a3d5388c79ff1a6fdb904daeff9b39d0bb7d26/src/main/java/com/android/volley/toolbox/BasicNetwork.java#L199][1]
You can get the exact error code using : #error.networkResponse.statusCode

How to force the code to go to onErrorResponse in Volley JsonObjectRequest

A crash is reported in Volley JsonObjectRequest and exactly in its onErrorResponse block. I cannot change the webservice output to test onErrorResponse block and it's important to debug this code block.
Can anyone tell me how to force the code to go to this part?
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, "MY_URL", null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
// Which code should I add here to force the code to go to onErrorResponse
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
...
// I want to debug this code block
}
});
I tried response = null; in onResponse but it caused a crash and it didn't go to onErrorResponse.
As #Vivek Mishra said in comments, I tried to set an invalid URL like "https://stackoverflow.com/" instead of "MY_URL" (which was a valid API URL), and it went to onErrorResponse.
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, "https://google.com/", null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
// OnResponse Codes
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// OnError Codes
}
});

Volley sending multiple requests Android

I am sending a request to server and it causes errors. I was able to trace the error to the fact that Volley is sending the request more than once. I searched the internet for solutions, I tried all what I came across but none of them seem to solve the problem
Below is my code:
public void btnLogOut(View view) {
final ProgressDialog loading = ProgressDialog.show(this, "Logging Out", "Please wait...", false, false);
//cover.setVisibility(View.GONE);
String token = dbHelper.getAuth().getString(0);
String IP = helperFunctions.getAppUrl();
final String url = IP + "/deregister?token=" + token+ "&appVersion=" + versionCode;
JsonObjectRequest sr = new JsonObjectRequest(Request.Method.POST, url,null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
loading.dismiss();
logOut.LogOutUser();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
if (VolleyErrorHelper.getMessage(error, Settings.this).equalsIgnoreCase("401")){
logOut.MakeUserLogin();
}else{
cover.setVisibility(View.VISIBLE);
}
//VolleyLog.e("Deregister GCM", "Error: " + error.getMessage());
loading.dismiss();
Toast.makeText(Settings.this, "Process not completed, try again!", Toast.LENGTH_LONG).show();;
}
});
sr.setRetryPolicy(new DefaultRetryPolicy(0, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(sr);
}
Try this:
sr.setRetryPolicy(new DefaultRetryPolicy(0, 0, DefaultRetryPolicy.DEFAULT_TIMEOUT_MS));
I used on the POST requests so it can do a retry

Another Volley request inside the callback listener of Volley request is not working in Android

I am developing an Android app. In my app I need to connect with server. So I am using Volley for it. But I have a little problem with Volley. Here is my code:
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, param, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
secondRequest();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
VolleySingleton.getInstance(getBaseContext()).addToRequestQueue(request);
void secondRequest()
{
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, param, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
VolleySingleton.getInstance(getBaseContext()).addToRequestQueue(request)
}
In my code above, the second request is not working. But the first request is working. My question is, Volley can be used to do the request like this? I mean another request inside the response of a request?

Simultaneous volley request always returns Server error

I am trying to develop an android application in which I am hitting the server with an API using volley. I have made a singleton RequestQueue object and sending multiple JSON request one after other. When I send the first request, I receive the JSON object without any error. I am then parsing the JSON, extracting some ids and again making a url and making a volley request. This time, I am always getting "com.android.volley.ServerError". Below is my code:
JsonObjectRequest jReq = new JsonObjectRequest(Request.Method.GET, url, (String) null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject jsonObject) {
final VolleyParser vp = new VolleyParser();
ArrayList<Integer> searchIds = vp.parseJSONObject(jsonObject, kf, null);
r.cancelAll("saz");
for(Integer id : searchIds) {
final Fetch pf = new ProductFetch();
String url = pf.searchURL(id.toString());
JsonObjectRequest jReqNext = new JsonObjectRequest(Request.Method.GET, url, (String) null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject jsonArray) {
Log.i("saz", "product rec");
vp.parseJSONObject(jsonArray, pf, null);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
Log.i("saz", "pro "+volleyError.toString());
}
});
/**//*(Request.Method.POST, url, (String) null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject jsonObject) {
Log.i("saz","product rec");
vp.parseJSONObject(jsonObject, pf, null);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
Log.i("saz", volleyError.toString());
}
});*/
jReqNext.setRetryPolicy(new DefaultRetryPolicy(
9000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
r.add(jReqNext);
}
}}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
Log.i("saz", "Search "+volleyError.toString());
}
});
r.add(jReq);
}
Please give more explanation what the server error is but it looks like that you are getting the Volley Server Timeout error. Please increase the request timeout to one min and see the results. Increase the timeout as
yourRequest.setRetryPolicy(new DefaultRetryPolicy(60000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

Categories

Resources