Flask接口签名sign原理与实例代码浅析
547
2023-06-28
Android异常 java.lang.IllegalStateException解决方法
android异常详情介绍
这种异常我遇到以下两种情况:
1. java.lang.IllegalStateException: No wrapped connection.
2.java.lang.IllegalStateException: Adapter is detached.
原因:
1.单线程一次执行一个请求可以正常执行,如果使用多线程,同时执行多个请求时就会出现连接超时.
2.HttpConnection没有连接池的概念,多少次请求就会建立多少个IO,在访问量巨大的情况下服务器的IO可能会耗尽。
3.通常是因为HttpClient访问单一实例的不同的线程或未关闭InputStream的httpresponse。
解决方案:获得httpclient线程安全
解决前代码:
public HttpClient hnbwgeLMGttpClient = newhttp:// DefaultHttpClient();
public void postNoResult(final Context context, final String url, final Map
new Thread() {
@Override
public void run() {
try {
HttpPost post = new HttpPost(url);
List
for (String key : maps.keySet()) {
params.add(nbwgeLMGnew BasicNameValuePair(key, maps.get(key)));
}
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse response = httpClient.execute(post);//报错位置
if (response.getStatusLine().getStatusCode() == 200) {
Looper.prepare();
String r = EntityUtils.toString(response.getEntity());
ToastUtil.print_log(r);
if (show != null) {
ToastUtil.show(context, show);
}
Looper.loop();}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}}}.start();
}
解决后代码:
public HttpClient httpClient = getThreadSafeClient();//获得httpclient线程安全。
public static DefaultHttpClient getThreadSafeClient() {
//获得httpclient线程安全的方法
DefaultHttpClient client = new DefaultHttpClient();
ClientConnectionManager mgr = client.getConnectionManager();
HttpParams params = client.getParams();
client = new DefaultHttpClient(new ThreadSafeClientConnManager(params,
mgr.getSchemeRegistry()), params);
return client;
}
public void postNoResult(final Context context, final String url, final Map
new Thread() {
@Override
public void run() {
try {
HttpPost post = new HttpPost(url);
List
for (String key : maps.keySet()) {
params.add(new BasicNameValuePair(key, maps.get(key)));
}
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse response = httpClient.execute(post);//报错位置
if (response.getStatusLine().getStatusCode() == 200) {
Looper.prepare();
String r = EntityUtils.toString(response.getEntity());
ToastUtil.print_log(r);
if (show != null) {
ToastUtil.show(context, show);
}
Looper.loop();}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}}}.start();
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~