當前位置: 首頁>>代碼示例>>Java>>正文


Java OkApacheClient類代碼示例

本文整理匯總了Java中com.squareup.okhttp.apache.OkApacheClient的典型用法代碼示例。如果您正苦於以下問題:Java OkApacheClient類的具體用法?Java OkApacheClient怎麽用?Java OkApacheClient使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


OkApacheClient類屬於com.squareup.okhttp.apache包,在下文中一共展示了OkApacheClient類的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: setupHttpClient

import com.squareup.okhttp.apache.OkApacheClient; //導入依賴的package包/類
static private HttpClient setupHttpClient() {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(30, TimeUnit.SECONDS);
    client.setFollowRedirects(false);
    OkApacheClient apacheClient = new OkApacheClient(client);

    return apacheClient;
}
 
開發者ID:janrain,項目名稱:jump.android,代碼行數:9,代碼來源:AsyncHttpClient.java

示例2: apacheAlikeApiSample

import com.squareup.okhttp.apache.OkApacheClient; //導入依賴的package包/類
private static void apacheAlikeApiSample() throws Exception {
        System.out.println("::: It's a apache client alike api usage.");
        // ::: It's a apache client alike api usage.
        // Create a apache kind of http client which you can share in whole application
        OkApacheClient client = new OkApacheClient();
        client.impl().setHostnameVerifier(UTILS.createInsecureHostnameVerifier());
        client.impl().setSslSocketFactory(UTILS.createInsecureSslSocketFactory());
        // Config the preferred protocol for special host you prefer to invoke with special protocol. 
        client.configPreferredHostProtocol("118.186.217.31", Protocol.SPDY_3);
        // http://118.186.217.31 hosts with a spdy style http service, we create a get request for it
        HttpGet request = new HttpGet(new URL("http://118.186.217.31/").toURI());
//        HttpGet request = new HttpGet(new URL("https://www.cloudflare.com/").toURI());
        // then run it.
        HttpResponse response = client.execute(request);
        // should OUTPUT:
        // HTTP/1.1 200 OK [OkHttp-Selected-Protocol: spdy/3.1, server: nginx/1.5.11, date: \
        // Thu, 12 Jun 2014 09:09:34 GMT, content-type: text/html; charset=UTF-8, alternate-protocol:\
        // 443:npn-spdy/3, OkHttp-Sent-Millis: 1402564173672, OkHttp-Received-Millis: 1402564173708]\
        // HTTP/1.1 200 OK [OkHttp-Selected-Protocol: spdy/3.1, server: cloudflare-nginx, date: \
        // Wed, 18 Jun 2014 10:35:44 GMT, content-type: text/html; charset=UTF-8, set-cookie: \
        // __cfduid=dec44ce8ac515d613f7490568f39612f21403087744468; expires=Mon, 23-Dec-2019 23:50:00 GMT; \
        // path=/; domain=.cloudflare.com; HttpOnly, expires: Wed, 18 Jun 2014 14:35:44 GMT, cache-control: \
        // public, max-age=14400, pragma: no-cache, x-frame-options: DENY, vary: Accept-Encoding, \
        // strict-transport-security: max-age=31536000, cf-cache-status: HIT, cf-ray: 13c6d782e0ac0d31-LAX, \
        // OkHttp-Sent-Millis: 1403087740701, OkHttp-Received-Millis: 1403087741183]

        System.out.println(response);
        String actual = EntityUtils.toString(response.getEntity(), UTF_8);
        System.out.println(actual);
        System.out.println(":::\n");

        System.out.println("::: It's a Url connection alike api usage.");
        // ::: It's a Url connection alike api usage
        // firstly, we create a okhttp lib client for further using
        OkHttpClient ok = new OkHttpClient();
        ok.setHostnameVerifier(UTILS.createInsecureHostnameVerifier());
        ok.setSslSocketFactory(UTILS.createInsecureSslSocketFactory());
        // we config a host with a prefered portocol kind
        // actally http://118.186.217.31 hosts with a spdy style http service
//        ok.configPreferredHostProtocol("118.186.217.31", Protocol.SPDY_3);
        // with the customized client, create a url factory
        OkUrlFactory factory = new OkUrlFactory(ok);
        // open an connection from a URL
        HttpURLConnection connection = factory.open(new URL("https://118.186.217.31"));
        connection.connect();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), UTF_8));
        for (String line = null; (line = reader.readLine()) != null;) System.out.println(line);
        reader.close();
        connection.disconnect();
        System.out.println(":::\n");
    }
 
開發者ID:NannanZ,項目名稱:spdymcsclient,代碼行數:52,代碼來源:App.java


注:本文中的com.squareup.okhttp.apache.OkApacheClient類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。