在android使用httpclient时出现“SocketException: Broken Pipe”的解决方法
2017-06-16 16:14
1136 查看
原因分析:
1.客户端与服务器的链接已经关闭(可能是客户端,也可能使服务器端,一般是客户端主动关闭),客户端继续向服务端写数据;2.在使用httpclient的threadsafeconnectionmanager或者poolconnectionmanger的时候容易出现,原因是我们设置了连接获取数据超时的时间;
解决方法:
1.为你的httpclient添加retry handler,形如下代码:[java] view
plain copy
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:
[java] view
plain copy
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去重试进行连接;
http://blog.csdn.net/androidzhaoxiaogang/article/details/8153456?utm_source=tuicool&utm_medium=referral
相关文章推荐
- 在android使用httpclient时出现“SocketException: Broken Pipe”的解决方法
- Android_关于使用android_r21 出现swt.jar的错误解决方法
- android使用豆瓣API出现500错误及解决方法
- Eclipse使用网上工程出现错误“The import android cannot be resolved”错误解决方法
- android中,使用ant多渠道打包时出现Perhaps JAVA_HOME does not point to the JDK问题的解决方法
- 在android中使用proguard混淆代码出现“Conversion to Dalvik format failed with error 1”错误的解决方法
- Android 使用adb shell命令时出现sqlite3 : not found 问题 解决方法
- 在android中使用proguard混淆代码出现“Conversion to Dalvik format failed with error 1”错误的解决方法
- Mac中第一次使用Android Studio出现unable to access android sdk add-on list的解决方法
- 命令行下使用javah命令生成.h文件,出现“错误: 无法访问android.app.Activity 找不到android.app.Activity的类文件”的解决方法
- 在android中使用proguard混淆代码出现“Conversion to Dalvik format failed with error 1”错误的解决方法
- 在android中使用proguard混淆代码出现“Conversion to Dalvik format failed with error 1”错误的解决方法
- Android开发使用adb时出现adb server is out of date的解决方法
- android中通过添加view使用ViewPager出现的问题及解决方法
- 命令行下使用javah命令生成.h文件,出现“错误: 无法访问android.app.Activity 找不到android.app.Activity的类文件”的解决方法
- Android开发使用adb时出现adb server is out of date的解决方法
- Android 使用自带的HttpClient进行https请求出现403的解决过程记录
- 在android中使用proguard混淆代码出现“Conversion to Dalvik format failed with error 1”错误的解决方法
- Android开发中使用startActivityForResult()方法从Activity A跳转Activity B出现B退出时A也同时退出的解决办法
- Android使用HttpClient多线程访问网络错误解决方法之一