Stopping silent retries in HttpURLConnection

后端 未结 4 2587
独厮守ぢ
独厮守ぢ 2021-02-20 16:50

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 retry

4条回答
  •  情深已故
    2021-02-20 17:23

    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");
        }
    }
    

提交回复
热议问题