Android volley is sending information twice with i

2019-03-16 04:57发布

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?

6条回答
Bombasti
2楼-- · 2019-03-16 05:07

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

查看更多
时光不老,我们不散
3楼-- · 2019-03-16 05:23

edit the retry number to 1. it worked for me.

stringRequest.setRetryPolicy(new
          DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, 1, 
                           DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)
                );
查看更多
地球回转人心会变
4楼-- · 2019-03-16 05:24

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 ?

查看更多
SAY GOODBYE
5楼-- · 2019-03-16 05:27

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.

查看更多
手持菜刀,她持情操
6楼-- · 2019-03-16 05:28

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.

查看更多
可以哭但决不认输i
7楼-- · 2019-03-16 05:28

Try this one.

JsonObjectRequest jsonObjRequest = new JsonObjectRequest(...);
jsonObjRequest.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
查看更多
登录 后发表回答