Cause analysis:
1. The connection between the client and the server has been closed (it may be the client or the server, usually the client closes it), and the client continues to write data to the server;
2. It is easy to use threadsafeconnectionmanager or poolconnectionmanger of httpclient, because we set the timeout time for the connection to obtain data;
Solution:
1. Add retry handler for your httpclient, and the code is as follows:
HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() { @Override public boolean retryRequest(IOException arg0, int arg1, HttpContext arg2) { // retry a max of 5 times if (arg1 >= 3) { return false; } if (arg0 instanceof ch.boye.httpclientandroidlib.NoHttpResponseException) { return true; } else if (arg0 instanceof ch.boye.httpclientandroidlib.client.ClientProtocolException) { return true; } return false; } }; sHttpClient.setHttpRequestRetryHandler(retryHandler);
2. Handle socketexception:
InputStream in = null; try { final HttpResponse response = HttpManager.execute(context, post); final int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); if (entity != null) { in = entity.getContent(); return IOUtils.stream2String(in); } } else { post.abort(); mLog.error("http code: " + response.getStatusLine().getStatusCode()); } } catch (IOException ex) { post.abort(); } catch (RuntimeException ex) { post.abort(); throw ex; } finally { IOUtils.closeStream(in); }
Socketexcption is a subclass of ioexception. When I/O exceptions are found, the connection is closed and httpclient is used to retry the connection;