Android volley sending data twice

AndroidAndroid Volley

Android Problem Overview


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);
}

Android Solutions


Solution 1 - Android

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));

Solution 2 - Android

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));

Solution 3 - Android

This will work

 RetryPolicy mRetryPolicy = new DefaultRetryPolicy(
 0,
 DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
 DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);

 request.setRetryPolicy(mRetryPolicy);

Solution 4 - Android

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);
}

Solution 5 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShashikala ChavanView Question on Stackoverflow
Solution 1 - Androidρяσѕρєя KView Answer on Stackoverflow
Solution 2 - AndroidSneha PatelView Answer on Stackoverflow
Solution 3 - AndroidShubham GoelView Answer on Stackoverflow
Solution 4 - AndroidHanishaView Answer on Stackoverflow
Solution 5 - AndroidMahmudur RahmanView Answer on Stackoverflow