HttpClient on Android : NoHttpResponseException through UMTS/3G

后端 未结 3 820
Happy的楠姐
Happy的楠姐 2020-11-29 05:45

I have got my android app that uses HttpClient to reach my servlet deployed on my Tomcat. It is installed on my HTC Magic.

If I launch it when connected on Wifi : it

3条回答
  •  遥遥无期
    2020-11-29 06:16

    I ran into this issue as well. It only occurred sporadically, usually on an initial http request. Subsequent requests would work fine. Adding setUseExpectContinue didn't seem to work.

    The solution in my case was to add a retry handler that would retry the request on specific exceptions:

            HttpProtocolParams.setUseExpectContinue(client.getParams(), false);
    
            HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() {
    
                public boolean retryRequest(IOException exception, int executionCount,
                        HttpContext context) {
                    // retry a max of 5 times
                    if(executionCount >= 5){
                        return false;
                    }
                    if(exception instanceof NoHttpResponseException){
                        return true;
                    } else if (exception instanceof ClientProtocolException){
                        return true;
                    } 
                    return false;
                }
            };
            client.setHttpRequestRetryHandler(retryHandler);
    

提交回复
热议问题