欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android异常 java.lang.IllegalStateException解决方法

程序员文章站 2024-03-01 13:59:52
android异常详情介绍 这种异常我遇到以下两种情况: 1. java.lang.illegalstateexception: no wrapped connect...

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();
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。