Help us understand the problem. What is going on with this article?

DefaultHttpClientの作成

More than 5 years have passed since last update.
HttpClientUtitlity
/**
 * HttpClientUtitlity
 */
public class HttpClientUtil extends DefaultHttpClient {
    private DefaultHttpClient httpClient;

    public static HttpClientUtil getInstance() {
        return new HttpClientUtil();
    }

    private HttpClientUtil() {
        // httpsにも対応する
        SchemeRegistry schReg = new SchemeRegistry();
        schReg.register(new Scheme(HttpHost.DEFAULT_SCHEME_NAME, PlainSocketFactory.getSocketFactory(), 80));
        schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

        //タイムアウトやら好きなように設定
        HttpParams httpParams;
        httpParams = new BasicHttpParams();
        HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(httpParams, HTTP.UTF_8);
        HttpConnectionParams.setConnectionTimeout(httpParams, 20000);
        HttpConnectionParams.setSoTimeout(httpParams, 20000);

        httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, schReg), httpParams);
    }

    public DefaultHttpClient getHttpCllient() {
        return this.httpClient;
    }
}

使用するときのサンプル

UseHttpClientUtitlity
HttpClientUtil http_util = HttpClientUtil.getInstance();
DefaultHttpClient client = http_util.getHttpCllient();
HttpGet method = new HttpGet("http://www.google.com");
try{
    HttpResponse response = client.execute(method);
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
    }
}catch(Exception e){}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away