Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android volley is sending information twice with image upload

I am trying to send an image with my post data to my server from android. To accomplish this I base 64 encoded my image to string and sent it using the android volley library. This is causing problems though. For some reason it sometimes sends the post twice, and I cannot figure out why. Below is the function that is called to send the post request. I put a break mark at the String url = "http://domain.com/ajax_ws.php"; and then one at the protected Map<String, String> getParams() { What I found is the String url = ... is only being called once but when it sends two, the protected Map... is called twice. I can't find any documentation on the android volley so I don't know why this is happening. The bitmap is resized so the image string is somewhere between 100k and 200k characters consistently. I thought maybe it was a size issue but my server is receiving the images and decoding them and everything just fine.

 public void Sharing() {

    pd = ProgressDialog.show(getParent(), null, "Please Wait...");
    final String caption = mEtMessage.getText().toString();
    RequestQueue queue = Volley.newRequestQueue(this);
    String url = "http://domain.com/ajax_ws.php";
    StringRequest postRequest = new StringRequest(
            Request.Method.POST,
            url,
            new MyStringListener(),
            new MyErrorListener()
    ) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("token", "secretToken");
            params.put("mode", "createVoucher");
            params.put("user_id", ActivityLogin.id);
            params.put("deal_id", ActivitySharing.id_deal);
            params.put("user_id_company", ActivityRestaurantDetails.res.getId());
            params.put("user_img", pathImage);
            params.put("caption", caption);
            params.put("company_id", ActivityRestaurantDetails.res.getId());
            return params;

        }
    };
    queue.add(postRequest);
}

Any idea why this might be happening?

like image 683
tomjung Avatar asked Nov 15 '13 21:11

tomjung


4 Answers

I am able to solve this issue by two ways.

First is suggested by Snicolas. Changed the RetryPolicy. Simply set the timeout value to double of the default timeout. Worked fine. You can also try other values.

request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

Another way is by setting connection.setChunkedStreamingMode(0); in openConnection method HurlStack class.

I am creating my RequestQueue like this requestQueue = Volley.newRequestQueue(context, new HurlStack());

Hope it helps :)

like image 108
Arun Badole Avatar answered Nov 14 '22 20:11

Arun Badole


Resolution is to edit retry policy which is also explained here: (http://www.techstricks.com/avoid-multiple-requests-when-using-volley/).

However, if overriding does not work for you then, then you may like to revisit your volley caching logic. Because of the soft ttl in volley caching the result gets delivered from the cache and at the same time it queues another network request which also will return a result. And therefore, single request but two different results.

like image 44
Mansha Chuttani Avatar answered Nov 14 '22 22:11

Mansha Chuttani


Volley uses a RetryPolicy for processing requests which by defaults sends the request up to 3 times with an exponential backoff algorithm. Can it be that some request fail and are retried ? Do you get any error/success logs for the first call of the request ?

like image 39
Snicolas Avatar answered Nov 14 '22 22:11

Snicolas


The below fix worked for me. Those who work with HTTPS and volley should try this.

DefaultRetryPolicy  retryPolicy = new DefaultRetryPolicy(0, -1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        jsr.setRetryPolicy(retryPolicy);

Hope this will help you to resolve the issue.

like image 32
Ajay Kumar Meher Avatar answered Nov 14 '22 22:11

Ajay Kumar Meher