2

所以正如我所说,我在尝试运行时在这两个错误之间来回跳动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

4 回答 4

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);
于 2013-08-23T04:44:38.647 回答
4

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

于 2013-08-23T04:45:35.133 回答
0

试试这个..

// 使用你的 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;

    }
}
于 2014-12-13T06:32:49.467 回答
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;

}
于 2014-12-13T06:22:42.217 回答