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?
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.Another way is by setting
connection.setChunkedStreamingMode(0);
inopenConnection
methodHurlStack
class.I am creating my
RequestQueue
like thisrequestQueue = Volley.newRequestQueue(context, new HurlStack());
Hope it helps :)
edit the retry number to 1. it worked for me.
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 ?
The below fix worked for me. Those who work with HTTPS and volley should try this.
Hope this will help you to resolve the issue.
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.
Try this one.