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

时间:2013-08-23 04:37:41

标签: java android

正如我说的那样,当我试图运行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);
}
}

4 个答案:

答案 0 :(得分:9)

试试这个,

BasicHttpParams params = new BasicHttpParams();
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
DefaultHttpClient httpclient = new DefaultHttpClient(cm, params);

答案 1 :(得分:4)

这可能是因为没有关闭从InputStream's获得的HttpClient,特别是如果来自不同的线程......要么不读取整个内容,要么从两个不同的线程调用相同的HttpClient实例。

答案 2 :(得分:0)

我找到了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;

}

答案 3 :(得分:0)

试试这个..

//使用您的URL执行asynctask

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

// Asynctask回调方法

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)
        {
        }
    }

// Service Handler类

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;

    }
}