转 Android-使用单例模式实现自己的HttpClient工具类
publicclassCustomerHttpClient{
privatestaticfinalStringCHARSET=HTTP.UTF_8;
privatestaticHttpClientcustomerHttpClient;
privateCustomerHttpClient(){
}
publicstaticsynchronizedHttpClientgetHttpClient(){
if(null==customerHttpClient){
HttpParamsparams=newBasicHttpParams();
//设置一些基本参数
HttpProtocolParams.setVersion(params,HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params,
CHARSET);
HttpProtocolParams.setUseExpectContinue(params,true);
HttpProtocolParams
.setUserAgent(
params,
"Mozilla/5.0(Linux;U;Android2.2.1;en-us;NexusOneBuild.FRG83)"
+"AppleWebKit/553.1(KHTML,likeGecko)Version/4.0MobileSafari/533.1");
//超时设置
/*从连接池中取连接的超时时间*/
ConnManagerParams.setTimeout(params,1000);
/*连接超时*/
HttpConnectionParams.setConnectionTimeout(params,2000);
/*请求超时*/
HttpConnectionParams.setSoTimeout(params,4000);
//设置我们的HttpClient支持HTTP和HTTPS两种模式
SchemeRegistryschReg=newSchemeRegistry();
schReg.register(newScheme("http",PlainSocketFactory
.getSocketFactory(),80));
schReg.register(newScheme("https",SSLSocketFactory
.getSocketFactory(),443));
//使用线程安全的连接管理来创建HttpClient
ClientConnectionManagerconMgr=newThreadSafeClientConnManager(
params,schReg);
customerHttpClient=newDefaultHttpClient(conMgr,params);
}
returncustomerHttpClient;
}
}
原文链接:http://www.cnblogs.com/codingmyworld/archive/2011/08/17/2141706.html