Android異常 java.lang.IllegalStateException解決方法
Android異常詳情介紹
這種異常我遇到以下兩種情況:
1. java.lang.IllegalStateException: No wrapped connection.
2.java.lang.IllegalStateException: Adapter is detached.
原因:
1.單線程一次執(zhí)行一個請求可以正常執(zhí)行,如果使用多線程,同時執(zhí)行多個請求時就會出現(xiàn)連接超時.
2.HttpConnection沒有連接池的概念,多少次請求就會建立多少個IO,在訪問量巨大的情況下服務(wù)器的IO可能會耗盡。
3.通常是因為HttpClient訪問單一實例的不同的線程或未關(guān)閉InputStream的httpresponse。
解決方案:獲得httpclient線程安全
解決前代碼:
public HttpClient httpClient = new DefaultHttpClient();
public void postNoResult(final Context context, final String url, final Map<String, String> maps, final String show) {
new Thread() {
@Override
public void run() {
try {
HttpPost post = new HttpPost(url);
List<NameValuePair> params = new ArrayList<NameValuePair>();
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();
}
解決后代碼:
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<String, String> maps, final String show) {
new Thread() {
@Override
public void run() {
try {
HttpPost post = new HttpPost(url);
List<NameValuePair> params = new ArrayList<NameValuePair>();
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();
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- java.lang.IllegalStateException:方法有太多主體參數(shù)問題
- java.lang.IllegalStateException異常解決
- java.lang.IllegalStateException異常原因和解決辦法
- 解決tomcat出現(xiàn):java.lang.IllegalStateException:無輸出目錄問題
- Java中出現(xiàn)java.lang.IllegalStateException異常錯誤的解決
- 解決報錯:java.lang.IllegalStateException: Failed to execute CommandLineRunner問題
相關(guān)文章
Android中實現(xiàn)記事本動態(tài)添加行效果
記事本對我們每個人來說再熟悉不過,下面這篇文章主要給大家介紹了在Android中實現(xiàn)記事本動態(tài)添加行效果的相關(guān)資料,這是最近在開發(fā)中遇到的一個小需求,想著分享出來供大家參考學習,需要的朋友們下面來一起看看吧。2017-06-06
android使用include調(diào)用內(nèi)部組件的方法
這篇文章主要介紹了android使用include調(diào)用內(nèi)部組件的方法,涉及Android組件調(diào)用的相關(guān)技巧,需要的朋友可以參考下2015-05-05
RecyclerView優(yōu)雅實現(xiàn)復(fù)雜列表布局
這篇文章主要為大家詳細介紹了RecyclerView優(yōu)雅實現(xiàn)復(fù)雜列表布局,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-11-11
Android編程設(shè)計模式之Builder模式實例詳解
這篇文章主要介紹了Android編程設(shè)計模式之Builder模式,結(jié)合實例形式詳細分析了Android設(shè)計模式之Builder模式概念、功能、使用場景、用法及相關(guān)注意事項,需要的朋友可以參考下2017-12-12
Android編程動態(tài)修改RelativeLayout寬高的方法
這篇文章主要介紹了Android編程動態(tài)修改RelativeLayout寬高的方法,涉及Android動態(tài)布局的相關(guān)技巧,需要的朋友可以參考下2015-12-12
Android模塊化中數(shù)據(jù)傳遞/路由跳轉(zhuǎn)實現(xiàn)示例
這篇文章主要介紹了Android模塊化中數(shù)據(jù)傳遞/路由跳轉(zhuǎn)實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07

