I am using Volley Network Library in my application.
The Issue is that it is sending data more than once when network connection is slow.
And After I Google this issue, all i can find about this issue is below point:
connection.setChunkedStreamingMode(0);
But I am not able to edit my volley library Hurlkstack classes.
It says:
The jar of this class file belong to container android Private libraries which does not allow modification to source attachments on it entries.
What should i do can some one help me
i have the following code where should i modify
.
private void makeJsonObjectRequest() {
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
"http://example.com/***.php", obj, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
response.getString("success");
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
No need to use connection.setChunkedStreamingMode(0); to avoid volley sending data twice bug. you need to set retry policy for current request :
JsonObjectRequest jsonObjReq = new JsonObjectRequest(...);
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(
0,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Use below code after completion of new Response.ErrorListener() method in your volley parsing. Hope its useful for you. I faced same issue and resolved it with the same code.
Code:
jsObjRequest.setRetryPolicy(new DefaultRetryPolicy(
30000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
This will work
RetryPolicy mRetryPolicy = new DefaultRetryPolicy(
0,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
request.setRetryPolicy(mRetryPolicy);
Try this will definitely work.
public <T> void addToRequestQueue(StringRequest req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
req.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
getRequestQueue().add(req);
}
I solve this problem to use this code. Set getCurrentRetryCount() to return 0 and Use HurlStack for BaseHttpStack.
RequestQueue requestQueue = Volley.newRequestQueue(NavigationActivity.this, new HurlStack());
requestQueue.add(stringRequest).setRetryPolicy(new RetryPolicy() {
#Override
public int getCurrentTimeout() {
return 5000;
}
#Override
public int getCurrentRetryCount() {
return 0; //retry turn off
}
#Override
public void retry(VolleyError error) throws VolleyError {
}
});
Hope this solve the problem.
Related
Volley request is done inside a while. It duplicates for no reason ( seemingly ) For example when I suppose to make two requests, volley does it 4 times. Below is my code, some of you could hint the problem in my code ?
while(i<chnumTxt.length()){
final RequestQueue queue;
queue = Volley.newRequestQueue(this);
char letter=chnumTxt.charAt(i);
Log.i("check","counter="+i+" "+"digit="+letter);
String URL = "http://192.168.4.20:80/chnumber?key="+letter;
Log.i("web",URL);
StringRequest request = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//Log.i("html",response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(request);
new CountDownTimer(500, 500) {
public void onFinish() {
// When timer is finished
// Execute your code here
}
public void onTick(long millisUntilFinished) {
}
}.start();
i++;
}
Eventually i found the answer somewhere. Volley will retry links if the link seems slow. So after blocking it, got it right. It is like :
First declare a variable :
static final float DEFAULT_BACKOFF_MULT = 1f;
and after defining the request, do below code :
request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
I'm trying to check a number in the server database with the volley .so I do not have any problem in Android Lollipop and above versions, I get result correctly but when I use KitKat version I don't get any result.
How can I fix it?
implementation 'com.android.volley:volley:1.0.0'
public class ChknumModel implements ChknumPre {
ChknumView _view;
Context _context;
public ChknumModel(ChknumView view,Context context){this._view = view;this._context = context;}
#Override
public void checknumber(String number) {
RequestQueue queue = Volley.newRequestQueue(_context);
StringRequest request = new StringRequest(Request.Method.GET,
DirectionU.BASE_URL_USERS + "chknumber/" + number + "/",
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
if(response.equals("found_number"))
_view.found_number();
else
_view.not_found_number();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("chknumber","error is " + error.getMessage());
}
});
request.setRetryPolicy(new DefaultRetryPolicy(
50000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(request);
}
}
This answer should be simple but I need help.
I add a url call to Volley and on error response I need to know what the URL was that the error is for, I may have 50 different URL's in a single queue for example so I want to know which one of the 50 returned the error.
Here is my code:
public void upload(String passurl) {
StringRequest stringRequest;
// Request a string response
stringRequest = new StringRequest(Request.Method.GET, passurl,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d(TAG,"Send successful");
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//***I want the URL that failed here!
error.printStackTrace();
}
});
stringRequest.setRetryPolicy(new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Add the request to the queue
stringRequest.setShouldCache(false);
stringRequest.setTag(TAG);
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(this.getApplicationContext());
mRequestQueue.start();
}
mRequestQueue.getCache().clear();
mRequestQueue.add(stringRequest);
}
To expand on #Levon's answer make passurl final for example method(final String passurl)
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));
I have recently started on Android development platform. I am using Android Studio, and using Volley library for network operation. I have implemented a backend for push notification for iOS and it is working very well, and now I am triggering this php via Volley network operation as follows.
For some reasons it calls that URL multiple times (5 or 6 times), how do I know? because iOS device receives multiple notifications. I am not sure why this happens and how I could solve it?
public void onClick(View v) {
if(v == buttonBuy) {
message = (EditText) findViewById(R.id.offerText);
buy();
}
}
private void buy()
{
StringRequest postRequest = new StringRequest(Request.Method.POST, Config.ASK_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
} catch (Exception e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}
) {
#Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<>();
params.put("token",token);
params.put("pname",objee.optString("pname"));
params.put("toid",objee.optString("uid"));
params.put("pid",pid);
params.put("message",message.getText().toString().trim());
return params;
}
};
CustomVolleyRequest.getInstance(this).getRequestQueue().add(postRequest);
}
Try to add the following code:
int socketTimeout = 10000; //10 seconds - change to what you want
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
postRequest.setRetryPolicy(policy);