16

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 ?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Hubert
  • 829
  • 1
  • 8
  • 15
  • Have you checked your server logs to see if the request is making it over the wire? If so, do your logs tell you anything (e.g., server-side error)? – CommonsWare Jan 12 '10 at 22:06
  • The server gets the request (very quickly) and gives a response successfully, in the very same way it does for request coming when on wifi. It would be more efficient to be able to debug HttpClient... The exception is thrown after a long time, more or less 10 minutes, so I guess, there is really nothing that comes back to the device. If true, something is loosing my response in the air. Could my GSM network have some filtering ? – Hubert Jan 14 '10 at 19:46
  • Interestingly, there is a squid server installed on proxy.cwg.net that seems to be the cause. But why does it work with the test HTML page and not with the HttpClient under same condition ? – Hubert Jan 16 '10 at 09:43

3 Answers3

15

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);
P. Taylor Goetz
  • 1,141
  • 7
  • 7
  • This is exactly the problem I am having.. unfortunately, the server gets the first request and changes some state.. sending that same request again really borks things.. – danb Jan 23 '12 at 16:38
  • @PTG Not working in my case. I set max executionCount to 50 to check if works after so many retries. But it did not. Any other idea? BTW, I use it with proxy and then only getting this exception. – Geek Jun 27 '13 at 07:59
11

I finally got rid of this problem : simply a HTTP header that was badly handled by a squid server on the road :

Expect: 100-Continue

It seems to be there by default with DefaultHttpClient on android SDK. To tackle this, simply add that in your code :

  HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), false);
Hubert
  • 829
  • 1
  • 8
  • 15
3

Android had a bug where HTTPS made reverse DNS lookups (fixed in Ice Cream Sandwich)

http://code.google.com/p/android/issues/detail?id=13117 mentioned a work around which works on my phone.

fishautumn
  • 364
  • 3
  • 6