httpClient访问网络,httpclient.execute(httpGet)方法不执行问题
2016-01-20 13:10
501 查看
最近在维护项目过程中遇到了一个比较奇葩的问题,软件在别的手机上都是OK的,就是在HTC手机上的时候,会遇到软件没有完全退出,然后再次点击软件,进不去的问题
首先说一下这个Bug产生的原因,因为每次 我们软件进入的时候都去请求GuidePage,看看是否需要升级,如果需要升级的话就要去请求引导界面,然后设置给Viewpager。那么
问题就产生在请求网络的时候,Bug复现;1,如果系统完全退出,就是在点击退出应用的时候调用System.exit(0)这个方法的时候,第二次进入软件的时候是没有问题的;问题在
于,没有完全退出应用,适当的保留一些服务的时候,再次进入就会进不去。根据log日志,分析出问题产生的原因就是在访问引导界面接口的时候,httpclient的execute方法没有
执行,观察自己封装的访问网络框架,发现链接都已经释放,httpclient也释放了,还是没能解决问题。最后确定问题在于访问网络的线程阻塞了。长时间不能相应。设置的链接超
时当然也是没有用的。下面就来说一下我的解决方案吧
public static String sendGetCommand(Context context, String url,
String retDataType) {
String result = null;
url = url.replaceAll(" ", "%20");
try {
httpRequest = new HttpGet(url);
// if(url.toLowerCase().startsWith("https"))
{
if (httpClient == null) {
httpClient = HttpsSSLSocketFactory.createMyHttpClient(
CONNECT_TIMEOUT, SO_TIMEOUT);
}
}
/*
* else { HttpParams httpParameters = new BasicHttpParams(); // Set
* the timeout in milliseconds until a connection is established. //
* The default value is zero, that means the timeout is not used.
* HttpConnectionParams.setConnectionTimeout(httpParameters,
* CONNECT_TIMEOUT); // Set the default socket timeout (SO_TIMEOUT)
* // in milliseconds which is the timeout for waiting for data.
* HttpConnectionParams.setSoTimeout(httpParameters, SO_TIMEOUT); if
* (httpClient == null) { httpClient = new
* DefaultHttpClient(httpParameters); } }
*/
if(httpClient != null){
Thread thread = new Thread(){
@Override
public void run() {
try {
response = httpClient.execute(httpRequest);
}catch(IllegalArgumentException ec){
response = null;
interrupted();
}
catch (ClientProtocolException e) {
response = null;
interrupted();
e.printStackTrace();
} catch (IOException e) {
response = null;
interrupted();
e.printStackTrace();
}
}
};
thread.start();
try {
Thread.sleep(1000*2);
if(response == null){
thread.interrupt();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int statusCode = response.getStatusLine().getStatusCode();
System.out.println(statusCode);
if (response.getStatusLine().getStatusCode() == 200) {
if (retDataType != null)
result = EntityUtils.toString(response.getEntity(),
retDataType);
else
result = EntityUtils.toString(response.getEntity(),
HTTP.UTF_8);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}finally{
httpClient.getConnectionManager().closeExpiredConnections();
}
return result;
}
在执行execute方法的时候,另外开启一个线程去执行,然后捕获异常,判断线程是否阻塞,然后让线程沉睡2秒钟,调用thread.interrupt();打断状态,这样就能解决这个bug了
首先说一下这个Bug产生的原因,因为每次 我们软件进入的时候都去请求GuidePage,看看是否需要升级,如果需要升级的话就要去请求引导界面,然后设置给Viewpager。那么
问题就产生在请求网络的时候,Bug复现;1,如果系统完全退出,就是在点击退出应用的时候调用System.exit(0)这个方法的时候,第二次进入软件的时候是没有问题的;问题在
于,没有完全退出应用,适当的保留一些服务的时候,再次进入就会进不去。根据log日志,分析出问题产生的原因就是在访问引导界面接口的时候,httpclient的execute方法没有
执行,观察自己封装的访问网络框架,发现链接都已经释放,httpclient也释放了,还是没能解决问题。最后确定问题在于访问网络的线程阻塞了。长时间不能相应。设置的链接超
时当然也是没有用的。下面就来说一下我的解决方案吧
public static String sendGetCommand(Context context, String url,
String retDataType) {
String result = null;
url = url.replaceAll(" ", "%20");
try {
httpRequest = new HttpGet(url);
// if(url.toLowerCase().startsWith("https"))
{
if (httpClient == null) {
httpClient = HttpsSSLSocketFactory.createMyHttpClient(
CONNECT_TIMEOUT, SO_TIMEOUT);
}
}
/*
* else { HttpParams httpParameters = new BasicHttpParams(); // Set
* the timeout in milliseconds until a connection is established. //
* The default value is zero, that means the timeout is not used.
* HttpConnectionParams.setConnectionTimeout(httpParameters,
* CONNECT_TIMEOUT); // Set the default socket timeout (SO_TIMEOUT)
* // in milliseconds which is the timeout for waiting for data.
* HttpConnectionParams.setSoTimeout(httpParameters, SO_TIMEOUT); if
* (httpClient == null) { httpClient = new
* DefaultHttpClient(httpParameters); } }
*/
if(httpClient != null){
Thread thread = new Thread(){
@Override
public void run() {
try {
response = httpClient.execute(httpRequest);
}catch(IllegalArgumentException ec){
response = null;
interrupted();
}
catch (ClientProtocolException e) {
response = null;
interrupted();
e.printStackTrace();
} catch (IOException e) {
response = null;
interrupted();
e.printStackTrace();
}
}
};
thread.start();
try {
Thread.sleep(1000*2);
if(response == null){
thread.interrupt();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int statusCode = response.getStatusLine().getStatusCode();
System.out.println(statusCode);
if (response.getStatusLine().getStatusCode() == 200) {
if (retDataType != null)
result = EntityUtils.toString(response.getEntity(),
retDataType);
else
result = EntityUtils.toString(response.getEntity(),
HTTP.UTF_8);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}finally{
httpClient.getConnectionManager().closeExpiredConnections();
}
return result;
}
在执行execute方法的时候,另外开启一个线程去执行,然后捕获异常,判断线程是否阻塞,然后让线程沉睡2秒钟,调用thread.interrupt();打断状态,这样就能解决这个bug了
相关文章推荐
- cod pods安装过程中Error fetching http://ruby.taobao.org/: bad response Not Found 404 (http://ruby.taobao
- Httpie 进行web请求模拟
- c++网络编程之socket
- 通过curl得到http各阶段的响应时间
- 18Java语法回顾之网络编程
- K-means,KNN,贝叶斯,贝叶斯网络
- ServerXMLHTTP的setTimeouts超时设置
- Linux网络编程:基于TCP的程序开发回顾篇
- HLS(HTTP Live Streaming)协议之m3u8文件生成方式
- android基础(五)网络编程
- tomcat配置https单向认证笔记
- graphite安装笔记
- 学习ios网络地址
- ssl介绍以及双向认证和单向认证原理
- 使用tcpcopy导入线上流量进行功能和压力测试
- App中HTTPS设置
- AJAX学习笔记(三)——XMLHttpRequest 对象
- Linux:网络yum源设置
- 附录_HTTP状态码
- TCP连接状态详解及TIME_WAIT过多的解决方法