HttpClient on Android: NoHttpResponseException root cause In my project, right?

I use DefaultHttpClient to retrieve data from the server, but a NoHttpResponseException exception occurred sporadically.

I referenced this post link and

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

not working for me.

I use HttpRequestRetryHandler to avoid this problem and it works, but I do not think this is the ideal method because the network data packet is dropped by tcpdump on the server side.

I found that when a NoHttpResponse exception occurs, the 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 did not find any tcp 3-way handhake data package when a new port from the client side connects to the server side. But based on tcp theory, shaking hands is a must when creating a new connection.

I don't know if this is an android bug (I use android 2.3 for testing), who has an idea? Thanks in advance.

+4
source share
1 answer

I recommend that you check your version of HttpClient, although it is much more widely used, HttpClient on android is not supported by the android command. You are probably using an older version. Here are the steps:

  • Make sure you get the latest version - http://hc.apache.org/downloads.cgi
  • Extract httpcomponents-client-4.2.1-bin to a convenient directory in your workspace.
  • Create a custom library called "Apache HttpClient 4.2.1" and specify in the .jar files (if necessary, add the source documents) and select the export on the Order and Export page for the library.
  • Link the library to your project.



Also check your connection settings. UseExpectContinue is recommended to be enabled for performance in the apache documentation (see http://hc.apache.org/httpclient-3.x/performance.html ). If you use HTTP_1_0 or less, that may cause a problem for you.

These settings work for me (note the use of HTTP_1_1, also your timeouts will probably be different 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); 
+1
source

Source: https://habr.com/ru/post/1385855/


All Articles