4

I use DefaultHttpClient to fetch data from server, but NoHttpResponseException occurred sporadically.

I refered to this post a link, and

HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), false);

does not work for me.

I use HttpRequestRetryHandler to avoid this problem, and it works, but I don't think this is a perfect method because of the network data package dumped by tcpdump on the server side.

I found that when NoHttpResponseException occured, an new port is used when connecting to the server instead of the old port used before this request(HttpClient does not reuse the old connection). But I didn't find any tcp 3-way handshake data package when the new port from client side connecting to the server side. But based on the tcp theory, handshake is the must step when making an new connection.

I don't know whether it is the bug of android(I'm using android 2.3 for testing), does anyone have idea? Thanks in advance.

Community
  • 1
  • 1
cmoaciopm
  • 2,076
  • 2
  • 22
  • 30

1 Answers1

1

I recommend that you check your HttpClient version, even though it is much more widely used, HttpClient on android is not maintained by the android team. You are probably running an older version. Here are the steps:

  1. Make sure you get the latest version - http://hc.apache.org/downloads.cgi
  2. Extract the httpcomponents-client-4.2.1-bin to a handy directory in your workspace
  3. Create a User Library named 'Apache HttpClient 4.2.1' and link in the .jar files (attach source docs too if you wish) and select export on the Order and Export page for the library.
  4. Link the library to your project.


Also, check your connection parameter settings. UseExpectContinue is recommended to be turned on for performance per the apache documentation (see http://hc.apache.org/httpclient-3.x/performance.html). If you are using HTTP_1_0 or less that could cause a problem for you.

These settings work for me (Note the use of HTTP_1_1, also your timeouts probably will differ from mine):

        HttpParams params = new BasicHttpParams();

        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
        HttpProtocolParams.setUseExpectContinue(params, true);

        HttpConnectionParams.setStaleCheckingEnabled(params, false);
        HttpConnectionParams.setConnectionTimeout(params, 13000);
        HttpConnectionParams.setSoTimeout(params, 30000);
        HttpConnectionParams.setSocketBufferSize(params, 8192);
Community
  • 1
  • 1
RightHandedMonkey
  • 1,718
  • 20
  • 25