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
Related
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
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));
i am developing an application that request data from MYSQL
i used an API (json) to retrieve the data from database.
My problem is that my project works fine on android studio but when i am running it on real device (Samsung GT-I9082) i got an exception and i don't know why!
the exception is:
java.io.EOFexception
mycode:
private void getData(String leng) {
final String id="23119574";
try {
if (id.equals("")) {
Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
return;
}
loading = ProgressDialog.show(this, "Please wait...", "Fetching...", false, false);
// String url = config.DATA_URL3+id;
String url = config.DATA_URL4 + id;
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response, id);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Handler_Barcode.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
stringRequest.setRetryPolicy((new DefaultRetryPolicy(
20000,
0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)));
requestQueue.add(stringRequest);
}catch(Exception e){
e.printStackTrace();
}
}
note: i am using volley
and my device android version is 4.2.2
thank you!
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));
The following is my code. I get com.android.volley.NoConnectionError: java.io.InterruptedIOException for the first time and for the second time it works fine. There server response is also fine, No error in server side.
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
JsonObjectRequest request = new JsonObjectRequest(URL, null,
new Listener<JSONObject>() {
#Override
public void onResponse(JSONObject responseJsonObject) {
try {
if (responseJsonObject.has("user")
&& !responseJsonObject
.isNull("user")) {
user.jsonParser(responseJsonObject
.getJSONObject("user"));
}
} catch (JSONException exception) {
Toast.makeText(context, "Error Occured",
Toast.LENGTH_SHORT).show();
}
}
}, new ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
progressDialog.dismiss();
Log.d("Volley Error", volleyError.toString());
Toast.makeText(context, "Connectivity Error",
Toast.LENGTH_SHORT).show();
}
});
queue.add(request);
progressDialog.show();
queue.start();
I had the same problem and I solved it by removing the following line
queue.start()
I was having the same issue. The question Vamsi referenced has a workaround using OkHttp that you works if you use OkHttp as the stack in volley (see also the comments in that question on how to use OkHttp in volley).
However, here is another hacky workaround that doesn't involve using another library:
final RequestQueue requestQueue = getRequestQueue();
final class ErrorFirstTimeHack {
boolean first_time = true;
MyVolleyJsonObjectRequest request = null;
}
final ErrorFirstTimeHack errorFirstTimeHack = new ErrorFirstTimeHack();
final MyVolleyJsonObjectRequest request = buildRequestObject(
Request.Method.GET,
url,
new Response.Listener() {
#Override
public void onResponse(Object response) {
// normal success processing
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
if (error instanceof com.android.volley.NoConnectionError) {
if (errorFirstTimeHack.first_time) {
errorFirstTimeHack.first_time = false;
requestQueue.add(errorFirstTimeHack.request);
return;
}
}
// normal error processing
}
}
);
errorFirstTimeHack.request = request;
requestQueue.add(request);