原因分析:
1.客户端与服务器的链接已经关闭(可能是客户端,也可能使服务器端,一般是客户端主动关闭),客户端继续向服务端写数据;
2.在使用httpclient的threadsafeconnectionmanager或者poolconnectionmanger的时候容易出现,原因是我们设置了连接获取数据超时的时间;
解决方法:
1.为你的httpclient添加retryhandler,形如下代码:
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.处理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是IOException的子类,当发现有IO异常的时候主动关闭该连接,而又httpClient去重试进行连接;