10

I'm using HttpURLConnection on Android KitKat to POST some data to a server. The server takes a long time to respond, and the connection is silently retrying 1 to 3 times before timing out. I don't want it to retry, since the server acts on all of the requests, resulting in Bad Things(TM).

I've tried System.setProperty("http.keepAlive", "false") before opening the connection, but that doesn't help.

Abhineet Verma
  • 1,008
  • 8
  • 18
Erik Browne
  • 1,393
  • 1
  • 14
  • 19
  • Have you tried [setReadTimeout](http://docs.oracle.com/javase/7/docs/api/java/net/URLConnection.html#setReadTimeout(int))? If server response takes long but doesn't send any package til then, it could probably work. – hgoebl Jun 25 '14 at 20:29
  • Already using setReadTimeout(). – Erik Browne Jun 26 '14 at 20:03
  • Are you sure your using the native `HttpURLConnection` and not a custom implementation like `okhttp` or `Volley`? AFAIK the stock implementation does not retry on SocketTimeoutExceptions. – Jeffrey Mixon Nov 12 '14 at 23:15
  • The native HttpURLConnection is okhttp, which is the problem. – Erik Browne Nov 14 '14 at 00:38

4 Answers4

4

For POST calls set

httpURLConnection.setChunkedStreamingMode(0);

and this should fix the silent retries. The bug report and workaround can be found here.

Espen Riskedal
  • 1,425
  • 15
  • 28
  • For a deeper understanding for the problem [read this](http://stackoverflow.com/a/37675253/2061089). – oli Jun 07 '16 at 09:20
0
  • Implement a hard timeout yourself, and force close the HttpURLConnection by calling disconnect. This can be done from your Activity using android handler; If you use AsyncTask, you can simply call cancel or Thread.interrupt():

    new Handler().postDelayed(new Runnable() {
        public void run() {
            httpUrlConnTask.cancel(true);
        }
    }, timeout * 1000); 
    

    And in your httpUrlConnTask, call disconnect:

    if (isCancelled()) {
        urlConnection.disconnect();
        return;
    }
    

    You may have to do urlConnection in another internal child thread so you can do a while loop in asynctask monitoring for isCancelled. And a try..catch so you can close all the streams properly.

  • you already have keepAlive false, and readTimeout, consider adding connection timeout too. This will set the socket timeout.

ashoke
  • 6,441
  • 2
  • 26
  • 25
  • can you please post entire code im new to Handler, and suggest when to use Handler and AsyncTask – LMK Nov 11 '14 at 06:32
  • can confirm that calling urlConnection.disconnect(); solved atleast my issue of automated unwanted retries – Stas Stelle Mar 16 '15 at 09:58
-1

You need to set System.setProperty("sun.net.http.retryPost", "false")

AZ_
  • 21,688
  • 25
  • 143
  • 191
user207421
  • 305,947
  • 44
  • 307
  • 483
-4

Android’s HTTP Clients

Prior to Froyo, HttpURLConnection had some frustrating bugs. In particular, calling close() on a readable InputStream could poison the connection pool. Work around this by disabling connection pooling:

private void disableConnectionReuseIfNecessary() {
    // HTTP connection reuse which was buggy pre-froyo
    if (Integer.parseInt(Build.VERSION.SDK) < Build.VERSION_CODES.FROYO) {
        System.setProperty("http.keepAlive", "false");
    }
}
AZ_
  • 21,688
  • 25
  • 143
  • 191
  • Read more information here http://android-developers.blogspot.com/2011/09/androids-http-clients.html – AZ_ Mar 20 '15 at 03:36