android开发之异常java.lang.IllegalStateException: Adapter is detached.的解决办法

来源:互联网 发布:中科院心理研究所知乎 编辑:程序博客网 时间:2024/05/29 02:28

异常详情:

10-31 17:50:19.140: E/AndroidRuntime(13396): FATAL EXCEPTION: Thread-17604
10-31 17:50:19.140: E/AndroidRuntime(13396): java.lang.IllegalStateException: Adapter is detached.
10-31 17:50:19.140: E/AndroidRuntime(13396): at org.apache.http.impl.conn.AbstractPooledConnAdapter.assertAttached(AbstractPooledConnAdapter.java:90)
10-31 17:50:19.140: E/AndroidRuntime(13396): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:118)
10-31 17:50:19.140: E/AndroidRuntime(13396): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:461)
10-31 17:50:19.140: E/AndroidRuntime(13396): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-31 17:50:19.140: E/AndroidRuntime(13396): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-31 17:50:19.140: E/AndroidRuntime(13396): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-31 17:50:19.140: E/AndroidRuntime(13396): at com.Fonelay.utils.MyHttpClient.getJsonforString(MyHttpClient.java:88)
10-31 17:50:19.140: E/AndroidRuntime(13396): at com.Fonelay.choiceness.ChoicenessActivity$12.run(ChoicenessActivity.java:416)

10-31 17:50:19.140: E/AndroidRuntime(13396): at java.lang.Thread.run(Thread.java:838)


解决办法1:

solve one issue because of thread safe httpclient
I was using HttpClient to handle the http request and response. Previously I always debug on latest phones which are upper 4.0 everything works fine. One day I tried on my older phone Moto Defy running 2.3.7 from Cyanogenmod . There was one issue when I tried to post an image .


I got the log :


Invalid use of SingleClientConnManager: connection still allocated.


or


Caused by: java.lang.IllegalStateException: No wrapped connection.


or


Caused by: java.lang.IllegalStateException: Adapter is detached.


Searched in stackoverflow normally it is because of accessing single instance of HttpClient in different thread or did not close InputStream of httpresponse. 


Mine issue is the first one. 


So I learnt one solution to get a thread safe httpclient.


public static DefaultHttpClient getThreadSafeClient() {


       DefaultHttpClient client = new DefaultHttpClient();


       ClientConnectionManager mgr = client.getConnectionManager();


       HttpParams params = client.getParams();


       client = new DefaultHttpClient(new ThreadSafeClientConnManager(params,


              mgr.getSchemeRegistry()), params);


       return client;


}



解决办法2(这里只贴参考网址,自己去看):

1.百度或者谷歌搜“ThreadSafeClientConnManager”、“httpclient连接池”;

2.http://blog.csdn.net/shootyou/article/details/6415248

3.http://bbs.csdn.net/topics/390257271

4.http://itstarting.iteye.com/blog/1231195


1 0
原创粉丝点击