Created
April 23, 2012 13:06
-
-
Save johnkil/2470811 to your computer and use it in GitHub Desktop.
HttpClientProvider Source from Pro Android 4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.http.HttpVersion; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.conn.ClientConnectionManager; | |
import org.apache.http.conn.params.ConnManagerParams; | |
import org.apache.http.conn.scheme.PlainSocketFactory; | |
import org.apache.http.conn.scheme.Scheme; | |
import org.apache.http.conn.scheme.SchemeRegistry; | |
import org.apache.http.conn.ssl.SSLSocketFactory; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; | |
import org.apache.http.params.BasicHttpParams; | |
import org.apache.http.params.HttpConnectionParams; | |
import org.apache.http.params.HttpParams; | |
import org.apache.http.params.HttpProtocolParams; | |
import org.apache.http.protocol.HTTP; | |
/** | |
* Source from Pro Android 4 | |
* | |
* @author e.shishkin | |
* | |
*/ | |
public class HttpClientProvider { | |
private static HttpClient mHttpClient; | |
/** A private Constructor prevents instantiation */ | |
private HttpClientProvider() {} | |
/** | |
* Getter method returns the one and only HttpClient object for the singleton, | |
* creating it the first time as necessary. | |
* @return HttpClient | |
*/ | |
public static synchronized HttpClient getHttpClient() { | |
if (mHttpClient == null) { | |
HttpParams params = new BasicHttpParams(); | |
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); | |
HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET); | |
HttpProtocolParams.setUseExpectContinue(params, true); | |
HttpProtocolParams.setUserAgent(params, | |
"Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; generic) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"); | |
ConnManagerParams.setTimeout(params, 1000); | |
// Set the timeout in milliseconds until a connection is established. | |
HttpConnectionParams.setConnectionTimeout(params, 5000); | |
// Set the default socket timeout (SO_TIMEOUT) in milliseconds which is the timeout for waiting for data. | |
HttpConnectionParams.setSoTimeout(params, 20000); | |
SchemeRegistry schReg = new SchemeRegistry(); | |
schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); | |
schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); | |
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg); | |
mHttpClient = new DefaultHttpClient(conMgr, params); | |
} | |
return mHttpClient; | |
} | |
public Object clone() throws CloneNotSupportedException { | |
throw new CloneNotSupportedException(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment