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

Jay*_*See 2 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

            ...
Run Code Online (Sandbox Code Playgroud)

另外,我的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);
}
}
Run Code Online (Sandbox Code Playgroud)

小智 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);
Run Code Online (Sandbox Code Playgroud)