Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
702 views
in Technique[技术] by (71.8m points)

http - HttpClient on Android : NoHttpResponseException through UMTS/3G

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 works. If I launch it when connected to 3G (GSM data network) : it doesn't work but my servlet is reached. In other word, it seems that my phone never get the response :

Technical problem while receiving response.
org.apache.http.NoHttpResponseException: The target server failed to respond
   at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:85)
   at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
   at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:179)
   at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
   at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
   at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
   at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
   at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:410)
   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)

If I use the web browser through 3G to activate the test HTML page that is packaged with my servlet, it reaches the same servlet with success (the page receives the response).

How could I debug HttpClient or ask it to dump everything ?

Does someone have a clue on what's going on ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...