編輯:關於Android編程
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 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();
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
Android Tab -- 使用ViewPager、PagerTitleStrip/PagerTabStrip來實現
效果:滑動切換;點擊標簽切換。 代碼:https://github.com/ldb-github/Layout_Tab1、使用ViewPager和PagerTa
Android UI之FrameLayout(幀布局)
Android UI之FrameLayout(幀布局)說明:幀布局會為每個包含其中的組件開辟一個空白區域(稱為幀),這些幀是一層層疊加在一起的,有點類似於一層層覆蓋貼上去
android實現多語言自動切換字體
我們建好一個android 的項目後,默認的res下面 有layout、values、drawable等目錄 這些都是程序默認的資源文件目錄,如果要實現多語言版本的話
翻翻git之---進度類自定義控件LoadingDrawable
這個控件之前就大致又看到V1版本的,然後坐著又做了更新,又寫了分析的博文,那這邊就簡單的把內容貼出來介紹給大家就好了(又幫我省事了)先來看下運行效果:內容都是手繪,可見原