Java 类com.squareup.okhttp.OkUrlFactory 实例源码

项目:GitTalent    文件:GithubImportService.java   
public GitHub initGithub() {
    String tmpDirPath = System.getProperty("java.io.tmpdir");
    File cacheDirectoryParent = new File(tmpDirPath);
    File cacheDirectory = new File(cacheDirectoryParent, "okhttpCache");
    if (!cacheDirectory.exists()) {
        cacheDirectory.mkdir();
    }
    Cache cache = new Cache(cacheDirectory, 100 * 1024 * 1024);
    try {
        return GitHubBuilder.fromCredentials()
                .withRateLimitHandler(RateLimitHandler.WAIT)
                .withAbuseLimitHandler(AbuseLimitHandler.WAIT)
                .withConnector(new OkHttpConnector(new OkUrlFactory(new OkHttpClient().setCache(cache))))
                .build();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
项目:XYZReader    文件:RemoteEndpointUtil.java   
static byte[] fetch(URL url) throws IOException {
    InputStream in = null;

    try {
        OkHttpClient client = new OkHttpClient();
        HttpURLConnection conn = new OkUrlFactory(client).open(url);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        in = conn.getInputStream();
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = in.read(buffer)) > 0) {
            out.write(buffer, 0, bytesRead);
        }
        return out.toByteArray();

    } finally {
        if (in != null) {
            in.close();
        }
    }
}
项目:droidddle    文件:IOUtil.java   
static String fetchPlainText(URL url) throws IOException {
    InputStream in = null;

    try {
        OkHttpClient client = new OkHttpClient();
        HttpURLConnection conn = new OkUrlFactory(client).open(url);
        conn.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT);
        conn.setReadTimeout(DEFAULT_READ_TIMEOUT);
        in = conn.getInputStream();
        return readFullyPlainText(in);

    } finally {
        if (in != null) {
            in.close();
        }
    }
}
项目:AndroidLiveWallpaperHelloWorld    文件:IOUtil.java   
static String fetchPlainText(URL url) throws IOException {
    InputStream in = null;

    try {
        OkHttpClient client = new OkHttpClient();
        HttpURLConnection conn = new OkUrlFactory(client).open(url);
        conn.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT);
        conn.setReadTimeout(DEFAULT_READ_TIMEOUT);
        in = conn.getInputStream();
        return readFullyPlainText(in);

    } finally {
        if (in != null) {
            in.close();
        }
    }
}
项目:LoboBrowser    文件:LoboBrowser.java   
/**
 * Initializes the global URLStreamHandlerFactory.
 * <p>
 * This method is invoked by {@link #init(boolean, boolean)}.
 */
public static void initProtocols(final SSLSocketFactory sslSocketFactory) {
  // Configure URL protocol handlers
  final PlatformStreamHandlerFactory factory = PlatformStreamHandlerFactory.getInstance();
  URL.setURLStreamHandlerFactory(factory);
  final OkHttpClient okHttpClient = new OkHttpClient();

  final ArrayList<Protocol> protocolList = new ArrayList<>(2);
  protocolList.add(Protocol.HTTP_1_1);
  protocolList.add(Protocol.HTTP_2);
  okHttpClient.setProtocols(protocolList);

  okHttpClient.setConnectTimeout(100, TimeUnit.SECONDS);

  // HttpsURLConnection.setDefaultSSLSocketFactory(sslSocketFactory);
  okHttpClient.setSslSocketFactory(sslSocketFactory);
  okHttpClient.setFollowRedirects(false);
  okHttpClient.setFollowSslRedirects(false);
  factory.addFactory(new OkUrlFactory(okHttpClient));
  factory.addFactory(new LocalStreamHandlerFactory());
}
项目:ActionLauncherApi    文件:IOUtil.java   
static String fetchPlainText(URL url) throws IOException {
    InputStream in = null;

    try {
        OkHttpClient client = new OkHttpClient();
        HttpURLConnection conn = new OkUrlFactory(client).open(url);
        conn.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT);
        conn.setReadTimeout(DEFAULT_READ_TIMEOUT);
        in = conn.getInputStream();
        return readFullyPlainText(in);

    } finally {
        if (in != null) {
            in.close();
        }
    }
}
项目:gdgapp    文件:GapiOkTransport.java   
@Override
protected GapiOkHttpRequest buildRequest(String method, String url) throws IOException {
    Preconditions.checkArgument(supportsMethod(method), "HTTP method %s not supported", method);
    // connection with proxy settings
    URL connUrl = new URL(url);
    OkHttpClient client = new OkHttpClient();
    OkUrlFactory factory = new OkUrlFactory(client);
    SSLContext sslContext;
    try {
        sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, null, null);
    } catch (GeneralSecurityException e) {
        throw new AssertionError(); // The system has no TLS. Just give up.
    }
    client.setSslSocketFactory(sslContext.getSocketFactory());

    if(proxy != null)
        client.setProxy(proxy);

    URLConnection conn = factory.open(connUrl);
    HttpURLConnection connection = (HttpURLConnection) conn;
    connection.setRequestMethod(method);

    return new GapiOkHttpRequest(connection);
}
项目:gdgapp    文件:OkStack.java   
@Override
protected HttpURLConnection createConnection(URL url) throws IOException {
    OkHttpClient client = new OkHttpClient();
    OkUrlFactory factory = new OkUrlFactory(client);
    SSLContext sslContext;
    try {
        TrustManager[] trustAllCerts = new TrustManager[] { new GdgTrustManager(App.getInstance().getApplicationContext()) };

        sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
    } catch (GeneralSecurityException e) {
        throw new AssertionError(); // The system has no TLS. Just give up.
    }
    client.setSslSocketFactory(sslContext.getSocketFactory());
    return factory.open(url);
}
项目:boohee_v5.6    文件:OkHttpStack.java   
public OkHttpStack(OkUrlFactory okUrlFactory) {
    this.mUrlRewriter = null;
    this.mSslSocketFactory = null;
    if (okUrlFactory == null) {
        throw new NullPointerException("Client must not be null.");
    }
    this.okUrlFactory = okUrlFactory;
}
项目:VoV    文件:OkHttpStack.java   
public OkHttpStack(OkHttpClient client) {
    if (client == null) {
        throw new NullPointerException("Client must not be null.");
    }
    client.interceptors().add(new GzipRequestInterceptor());
    mFactory = new OkUrlFactory(client);
}
项目:popcorn-android    文件:RecommendationContentProvider.java   
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
        throws FileNotFoundException {
    ParcelFileDescriptor[] pipe = null;

    String url = uri.getPath();

    try {
        String decodedUrl = URLDecoder.decode(url.replaceFirst("/", ""),
                "UTF-8");
        pipe = ParcelFileDescriptor.createPipe();

        OkHttpClient httpClient = PopcornApplication.getHttpClient();
        OkUrlFactory factory = new OkUrlFactory(httpClient);
        HttpURLConnection connection = factory.open(new URL(decodedUrl));

        new TransferThread(connection.getInputStream(),
                new ParcelFileDescriptor.AutoCloseOutputStream(pipe[1]))
                .start();
    } catch (IOException e) {
        Log.e(getClass().getSimpleName(), "Exception opening pipe", e);
        throw new FileNotFoundException("Could not open pipe for: "
                + uri.toString());
    }

    return (pipe[0]);
}
项目:Tower-develop    文件:NetworkUtils.java   
public static HttpURLConnection getHttpURLConnection(final URL url, final Cache cache, final SSLSocketFactory sslSocketFactory) {
    OkHttpClient client = new OkHttpClient();
    if (cache != null) {
        client.setCache(cache);
    }
    if (sslSocketFactory != null) {
        client.setSslSocketFactory(sslSocketFactory);
    }
    HttpURLConnection connection = new OkUrlFactory(client).open(url);
    connection.setRequestProperty("User-Agent", MapboxUtils.getUserAgent());
    return connection;
}
项目:fakeweibo    文件:OkHttpStack.java   
public OkHttpStack(OkHttpClient client) {
    if (client == null) {
        throw new NullPointerException("client cannot be null!");
    }
    okHttpClient = client;
    this.client = client;
    this.urlFactory = new OkUrlFactory(this.client);
}
项目:facebook-content-provider    文件:NetworkPipeContentProvider.java   
protected HttpURLConnection createConnection(String url) throws IOException {
    OkHttpClient okHttpClient = new OkHttpClient();
    //okHttpClient.setSslSocketFactory(HttpRequest.getTrustedFactory());
    //okHttpClient.setHostnameVerifier(HttpRequest.getTrustedVerifier());
    //okHttpClient.networkInterceptors().add(new StethoInterceptor());
    HttpURLConnection conn = new OkUrlFactory(okHttpClient).open(new URL(url));
    return conn;
}
项目:rastertheque    文件:NetworkUtils.java   
public static HttpURLConnection getHttpURLConnection(final URL url, final Cache cache, final SSLSocketFactory sslSocketFactory) {
    OkHttpClient client = new OkHttpClient();
    if (cache != null) {
        client.setCache(cache);
    }
    if (sslSocketFactory != null) {
        client.setSslSocketFactory(sslSocketFactory);
    }
    HttpURLConnection connection = new OkUrlFactory(client).open(url);
    connection.setRequestProperty("User-Agent", MapboxConstants.USER_AGENT);
    return connection;
}
项目:catnut    文件:OkHttpStack.java   
public OkHttpStack(OkHttpClient client) {
    if (client == null) {
        throw new NullPointerException("client cannot be null!");
    }
    okHttpClient = client;
    this.client = client;
    this.urlFactory = new OkUrlFactory(this.client);
}
项目:android-skeleton-project    文件:NetworkManager.java   
private OkUrlFactory generateDefaultOkUrlFactory() {
            OkHttpClient client = new com.squareup.okhttp.OkHttpClient();

            try {
                Cache responseCache = new Cache(baseContext.getCacheDir(), SIZE_OF_CACHE);
                client.setCache(responseCache);
            } catch (Exception e) {
//                Log.d(TAG, "Unable to set http cache", e);
            }

            client.setConnectTimeout(READ_TIMEOUT, TimeUnit.MILLISECONDS);
            client.setReadTimeout(CONNECT_TIMEOUT, TimeUnit.MILLISECONDS);
            return new OkUrlFactory(client);
        }
项目:OpenMapKitAndroid    文件:NetworkUtils.java   
public static HttpURLConnection getHttpURLConnection(final URL url, final Cache cache, final SSLSocketFactory sslSocketFactory) {
    OkHttpClient client = new OkHttpClient();
    if (cache != null) {
        client.setCache(cache);
    }
    if (sslSocketFactory != null) {
        client.setSslSocketFactory(sslSocketFactory);
    }
    HttpURLConnection connection = new OkUrlFactory(client).open(url);
    connection.setRequestProperty("User-Agent", MapboxUtils.getUserAgent());
    return connection;
}
项目:battery    文件:OkHttpStack.java   
public OkHttpStack(OkHttpClient client) {
    if (client == null) {
        throw new NullPointerException("Client must not be null.");
    }

    try {
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, null, null);
        client.setSslSocketFactory(sslContext.getSocketFactory());
    } catch (Exception e) {
        throw new AssertionError(); // The system has no TLS. Just give up.
    }

    mFactory = new OkUrlFactory(client);
}
项目:boohee_v5.6    文件:OkHttpStack.java   
public OkHttpStack() {
    this(new OkUrlFactory(new OkHttpClient()));
}
项目:xowa_android    文件:OkHttpConnectionFactory.java   
public HttpURLConnection create(URL url) throws IOException {
    return new OkUrlFactory(client).open(url);
}
项目:Sxumiro_AndroidClient    文件:OkHttpStack.java   
@Override
protected HttpURLConnection createConnection(URL url) throws IOException {
    OkUrlFactory okUrlFactory = new OkUrlFactory(okHttpClient);
    return okUrlFactory.open(url);
}
项目:fakeweibo    文件:OkHttpStack.java   
public static OkUrlFactory getOkUrlFactory() {
    return urlFactory;
}
项目:lunzi    文件:OkHttpStack.java   
public OkHttpStack(OkHttpClient client) {
    if (client == null) {
        throw new NullPointerException("Client must not be null.");
    }
    mFactory = new OkUrlFactory(client);
}
项目:brasilino    文件:OkHttpStack.java   
public OkHttpStack(OkHttpClient client) {
    if (client == null) {
        throw new NullPointerException("Client must not be null.");
    }
    mFactory = new OkUrlFactory(client);
}
项目:JianDan_OkHttpWithVolley    文件:OkHttpStack.java   
@Override
protected HttpURLConnection createConnection(URL url) throws IOException {
    OkUrlFactory okUrlFactory = new OkUrlFactory(okHttpClient);
    return okUrlFactory.open(url);
}
项目:shr    文件:OkHttpStack.java   
public OkHttpStack() {
  this(new OkUrlFactory(new OkHttpClient()));
}
项目:shr    文件:OkHttpStack.java   
public OkHttpStack(OkUrlFactory okUrlFactory) {
  if (okUrlFactory == null) {
    throw new NullPointerException("Client must not be null.");
  }
  this.okUrlFactory = okUrlFactory;
}
项目:progscrape-android    文件:FeedRequest.java   
private OkUrlFactory getUrlFactory() {
    return new OkUrlFactory(getOkHttpClient());
}
项目:EpiDroid    文件:OkHttpStack.java   
public OkHttpStack() {
    this(new OkUrlFactory(new OkHttpClient()));
}
项目:EpiDroid    文件:OkHttpStack.java   
public OkHttpStack(OkUrlFactory okUrlFactory) {
    if (okUrlFactory == null) {
        throw new NullPointerException("Client must not be null.");
    }
    this.okUrlFactory = okUrlFactory;
}
项目:GitRobot    文件:OkHttpConnector.java   
public OkHttpConnector(OkUrlFactory urlFactory) {
    this.urlFactory = urlFactory;
}
项目:Ok-Volley    文件:OkHttpStack.java   
public OkHttpStack(OkHttpClient client) {
    if (client == null) {
        throw new NullPointerException("Client must not be null.");
    }
    mFactory = new OkUrlFactory(client);
}
项目:TitanjumNote    文件:OkHttpStack.java   
public OkHttpStack(OkHttpClient client) {
    if (client == null) {
        throw new NullPointerException("Client must not be null.");
    }
    mFactory = new OkUrlFactory(client);
}
项目:offer    文件:OkUrlConnectionClient.java   
public OkUrlConnectionClient(OkHttpClient okhttpclient)
{
  mURLFactory = new OkUrlFactory(okhttpclient);
}
项目:spdymcsclient    文件:App.java   
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");
    }
项目:IceNet    文件:OkHttpStack.java   
public OkHttpStack(OkHttpClient client) {
    if (client == null) {
        throw new NullPointerException("Client must not be null.");
    }
    mFactory = new OkUrlFactory(client);
}
项目:something.apk    文件:OkHttpStack.java   
protected HttpURLConnection createConnection(URL url) throws IOException {
    return new OkUrlFactory(client).open(url);
}
项目:MagnetRestClient    文件:OkHttpStack.java   
public OkHttpStack(OkHttpClient client) {
    if (client == null) {
        throw new NullPointerException("Client must not be null.");
    }
    mFactory = new OkUrlFactory(client);
}
项目:youcast-android    文件:OkHttpStack.java   
public OkHttpStack(OkHttpClient client) {
    if (client == null) {
        throw new NullPointerException("OkHttpClient must not be null.");
    }
    mFactory = new OkUrlFactory(client);
}