I am trying to upload an image to my Imgur account using Volley
StringRequest
. I am not sure how to handle the response so what happens is it keeps re uploading since it hasnt received a response from Imgur. The image ends up being uploaded a couple of times until the TimeoutError
is thrown. How can I ensure this does not happen as I cannot detect the response being returned from the Imgur server?
Here is the method I am using to upload the image after converting it to Base64:
public void uploadImage(View view) {
Log.i(TAG,"start upload");
StringRequest uploadRequest = new StringRequest(Request.Method.POST, AppConst.IMGUR_ADD_IMG, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "finished image upload");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, error.toString());
Log.e(TAG,"finish/error upload");
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + token);
return headers;
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put(AppConst.IMGUR_TAG_IMAGE, encodeImage(selectedImg));
params.put(AppConst.IMGUR_TAG_TITLE, "title");
params.put(AppConst.IMGUR_TAG_NAME, String.valueOf(System.currentTimeMillis()));
return params;
}
};
AppController.getInstance().addToRequestQueue(uploadRequest);
}
Change the DefaultRetryPolicy on the Request to have a larger expiration time frame, since uploading an image can take a bit. Also, for the double upload, make sure to have max_retries set to 0.
new DefaultRetryPolicy(LONGER_TIMEOUT, 0, 0);