57

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);
}
Shashikala Chavan
  • 573
  • 1
  • 4
  • 9
  • See if this helps you http://stackoverflow.com/questions/20027995/android-volley-returning-results-twice-for-one-request – Rohit5k2 Jan 10 '15 at 05:04
  • 1
    Can you post how you post your data?? because i can just see the code for retrieve data using `JsonObjectRequest`. You can set retry policy for that `jsonObjReq.setRetryPolicy(new DefaultRetryPolicy( 0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));` – Piyush Jan 10 '15 at 05:39
  • @PiyushGupta i have got answer if it did'nt satisfy me than i will post more info thanks for replying – Shashikala Chavan Jan 10 '15 at 05:45
  • @ShashikalaChavan Okay. no problem. – Piyush Jan 10 '15 at 05:48
  • "DefaultRetryPolicy.DEFAULT_MAX_RETRIES = 0" then also not working at my end – Sonali Kale Apr 25 '20 at 05:33

5 Answers5

107

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));
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • 4
    @ShashikalaChavan: call `setRetryPolicy` before `AppController.getInstance().addToRequestQueue(jsonObjReq);` line – ρяσѕρєя K Jan 10 '15 at 05:40
  • hey i got your answer and it was correct thanks for the help i just want to ask is there any way in volley to avoid data get lost during post data – Shashikala Chavan Jan 12 '15 at 04:37
  • 1
    Is this solution may cause `java.lang.NegativeArraySizeException`? Is it possible? This is the problem ([link](http://stackoverflow.com/q/40318347/4074312)) I've encountered after I used setRetryPolicy – Orcun Sevsay Oct 29 '16 at 12:36
  • if we set request timeout to zero then when it will timeout the request – sunil kushwah Nov 02 '17 at 05:23
  • 3
    To avoid sending twice, set maxNumRetries to 0. (DEFAULT_MAX_RETRIES = 1 sot it will retry one time and send the message twice) – Lolo Jan 24 '18 at 14:26
  • Thanks a lot. Its also work on StringRequest ``` stringRequest.setRetryPolicy(new DefaultRetryPolicy( 0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); ``` – Pamungkas Jayuda May 03 '18 at 10:40
  • while thousands of people answering this, this thing never works – Zahan Safallwa May 18 '18 at 06:21
  • Thanks for the solution, but strangely for me only the new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT) worked! I ran tests on a Samsung-XCover Android 8.1.0 SDK-27 device. – iOS-Coder Nov 29 '18 at 15:13
  • Kotlin `request.setRetryPolicy(DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT))`, or as mentioned set max retires to zero: `request.setRetryPolicy(DefaultRetryPolicy(0, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT))` – Mike Irving Jan 28 '20 at 14:18
  • Im wondering why the hell android is logging the request only being sent once, but the server showing 2 requests. Thank you very much OP – Joe Feb 27 '20 at 16:45
  • I am using it in string request `request.setRetryPolicy(new DefaultRetryPolicy( 0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));` – Harikrushna Patel Jan 05 '22 at 07:50
7

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));
Sneha Patel
  • 393
  • 5
  • 7
3

This will work

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

 request.setRetryPolicy(mRetryPolicy);
Shubham Goel
  • 1,962
  • 17
  • 25
3

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);
}
Hanisha
  • 849
  • 10
  • 8
1

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.

Mahmudur Rahman
  • 638
  • 9
  • 25