-1

I am trying to make a post request in my android app to an external flask app with Volley and a MultipartEntity but the flask app is taking too long to respond and the Response.ErrorListener is getting called. I want to lengthen the timeout of the Response so the flask side has the time it needs to respond.

This is my MultipartEntity code:

    public MultipartRequest(String url, Response.ErrorListener errorListener, 
    Response.Listener<String> listener, File file, String stringPart)
    {
        super(Method.POST, url, errorListener);

        mListener = listener;
        mFilePart = file;
        mStringPart = stringPart;
        buildMultipartEntity();
    }

    private void buildMultipartEntity()
    {
        entity.addPart(FILE_PART_NAME, new FileBody(mFilePart));
    }

    @Override
    public String getBodyContentType()
    {
        return entity.getContentType().getValue();
    }

    @Override
    public byte[] getBody() throws AuthFailureError
    {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try
        {
            entity.writeTo(bos);
        }
        catch (IOException e)
        {
            VolleyLog.e("IOException writing to ByteArrayOutputStream");
        }
        return bos.toByteArray();
    }

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response)
    {
        return Response.success("Uploaded", getCacheEntry());
    }

    @Override
    protected void deliverResponse(String response)
    {
        mListener.onResponse(response);
    }
}

This is my send image function using that code:

public void sendImg(View v){
    File pic = (File) getIntent().getExtras().get("pic");
    String str = "img";
    MultipartRequest multipartRequest = new 
MultipartRequest("http://100.64.113.81/digitize",
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("img", "That didn't work!");
                }
            },
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.d("server", response);
                }
            }, pic, str);
    RequestQueue queue = Volley.newRequestQueue(this);
    queue.add(multipartRequest);
}

Thanks so much for your help.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Japhy
  • 45
  • 7
  • you can add this line end of function, `queue .setRetryPolicy(new DefaultRetryPolicy( MY_SOCKET_TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));` – Android Dec 03 '18 at 09:08
  • 1
    Possible duplicate of [Change Volley timeout duration](https://stackoverflow.com/questions/17094718/change-volley-timeout-duration) – Rohit5k2 Dec 03 '18 at 09:09
  • See this https://stackoverflow.com/questions/17094718/change-volley-timeout-duration – Rohit5k2 Dec 03 '18 at 09:10
  • 1
    Have you tried checking why the reponse takes so long? From my point of view, you should better fix that then working around that problem – Nico Haase Dec 03 '18 at 09:40

1 Answers1

0

Try this one

stringRequest.setRetryPolicy(new RetryPolicy() {
            @Override
            public int getCurrentTimeout() {
                return 10000;
            }

            @Override
            public int getCurrentRetryCount() {
                return 10000;
            }

            @Override
            public void retry(VolleyError error) throws VolleyError {

            }
        });
        MyApplication.getInstance().addToRequestQueue(stringRequest);
Hossam Hassan
  • 795
  • 2
  • 13
  • 39