间和QUOT反弹;适配器分离"和[否包裹连接"与HttpClient的 [英] Bouncing between "Adapter is detached" and "No wrapped connection" with HttpClient
本文介绍了间和QUOT反弹;适配器分离"和[否包裹连接"与HttpClient的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以当我说我弹来弹去这两个错误的尝试运行时, HttpClient.execute(HttpPost)
。获得 IllegalStateException异常
公共类NetMethods {
私有静态HttpClient的客户端=新DefaultHttpClient();
公共静态无效getStuff(){
ArrayList的<报警>报警=新的ArrayList<报警>();
HttpPost后= HttpPostFactory.getHttpPost(一个getStuff);
StringBuilder的建设者=新的StringBuilder();
HTT presponse响应= client.execute(后); //这里异常抛出
...
另外,我MttpPostFactory只是有这种
进口org.apache.http.client.methods.HttpPost;
公共类HttpPostFactory {
私有静态最后字符串URL =http://example.com/ExampleFolder/;
公共静态HttpPost getHttpPost(String s)将{
返回新HttpPost(URL + S);
}
}
解决方案
试试这个,
BasicHttpParams PARAMS =新BasicHttpParams();
SchemeRegistry schemeRegistry =新SchemeRegistry();
schemeRegistry.register(新计划(HTTP,PlainSocketFactory.getSocketFactory(),80));
最后的SSLSocketFactory的SSLSocketFactory = SSLSocketFactory.getSocketFactory();
schemeRegistry.register(新计划(https开头,SSLSocketFactory的,443));
ClientConnectionManager厘米=新ThreadSafeClientConnManager(参数,可以schemeRegistry);
DefaultHttpClient的HttpClient =新DefaultHttpClient(厘米,则params);
So as i said i'm bouncing back and forth between these two errors when trying to run HttpClient.execute(HttpPost)
. Getting 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
...
Also, my MttpPostFactory just has this
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);
}
}
解决方案
Try this,
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);
这篇关于间和QUOT反弹;适配器分离&QUOT;和[否包裹连接&QUOT;与HttpClient的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文