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

异常详情:

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
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值