使用 HttpClient 在"Adapter is detached"和"No wrapped connection"之间弹跳



正如我所说,当我试图运行HttpClient.execute(HttpPost)时,我在这两个错误之间来回跳动。获取IllegalStateException

public class NetMethods {
    private static HttpClient client = new DefaultHttpClient();
    public static void getStuff() {
    ArrayList<Alarm> alarms = new ArrayList<Alarm>();
    HttpPost post = HttpPostFactory.getHttpPost("GetStuff");
    StringBuilder builder = new StringBuilder();
    HttpResponse response = client.execute(post); // Exception thrown here
            ...

此外,我的MttpPostFactory只有这个

import org.apache.http.client.methods.HttpPost;
public class HttpPostFactory {
private static final String url = "http://example.com/ExampleFolder/";
public static HttpPost getHttpPost(String s) {
    return new HttpPost(url + s);
}
}

这可能是由于没有关闭从HttpClient获得的InputStream's而导致的,尤其是在来自不同线程的情况下。。。要么不读取整个内容,要么从两个不同的线程调用相同的HttpClient实例。

我从Androider的博客中找到了解决方案:

我得到了日志:

Invalid use of SingleClientConnManager: connection still allocated.

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

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

最终得到解决方案:

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

试试这个

//使用您的URL 执行异步任务

new myAsyncTask().execute("http://example.com/ExampleFolder/");

//异步任务回调方法

private class myAsyncTask extends AsyncTask<String, Void, Void>
    {
        protected void onPreExecute()
        {
            super.onPreExecute();
        }
        @Override
        protected Void doInBackground(String... arg0)
        {
            // Creating service handler class instance
            ServiceHandler serhand = new ServiceHandler();
            // Making a request to url and getting response
            serhand.makeServiceCall(arg0[0], ServiceHandler.GET);
            return null;
        }
        protected void onPostExecute(Void result)
        {
        }
    }

//服务处理程序类

package yourpagename
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class ServiceHandler {
    static String response = null;
    public final static int GET = 1;
    public final static int POST = 2;
    public ServiceHandler() {
    }
    /**
     * Making service call
     * @url - url to make request
     * @method - http request method
     * */
    public String makeServiceCall(String url, int method) {
        return this.makeServiceCall(url, method, null);
    }
    /**
     * Making service call
     * @url - url to make request
     * @method - http request method
     * @params - http request params
     * */
    public String makeServiceCall(String url, int method,
                                  List<NameValuePair> params) {
        try {
            // http client
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;
            // Checking http request method type
            if (method == POST) {
                HttpPost httpPost = new HttpPost(url);
                // adding post params
                if (params != null) {
                    httpPost.setEntity(new UrlEncodedFormEntity(params));
                }
                httpResponse = httpClient.execute(httpPost);
            } else if (method == GET) {
                // appending params to url
                if (params != null) {
                    String paramString = URLEncodedUtils
                            .format(params, "utf-8");
                    url += "?" + paramString;
                }
                HttpGet httpGet = new HttpGet(url);
                httpResponse = httpClient.execute(httpGet);
            }
            httpEntity = httpResponse.getEntity();
            response = EntityUtils.toString(httpEntity);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }
}

最新更新