下面列出了com.squareup.okhttp.Cache#com.squareup.okhttp.OkUrlFactory 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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();
}
}
}
/**
* 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 StreamHandlerFactory factory = StreamHandlerFactory.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());
}
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);
}
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);
}
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;
}
public OkHttpStack(OkHttpClient client) {
if (client == null) {
throw new NullPointerException("Client must not be null.");
}
mFactory = new OkUrlFactory(client);
}
@Override
protected HttpURLConnection createConnection(URL url) throws IOException {
OkUrlFactory okUrlFactory = new OkUrlFactory(okHttpClient);
return okUrlFactory.open(url);
}
public OkHttpStack(OkHttpClient client) {
if (client == null) {
throw new NullPointerException("Client must not be null.");
}
mFactory = new OkUrlFactory(client);
}
public OkHttpStack(OkHttpClient client) {
if (client == null) {
throw new NullPointerException("Client must not be null.");
}
mFactory = new OkUrlFactory(client);
}
protected HttpURLConnection createConnection(URL url) throws IOException {
return new OkUrlFactory(client).open(url);
}
public static OkUrlFactory getOkUrlFactory() {
return urlFactory;
}
private OkHttpConnector createCachedHttpConnector() {
final File cacheDirectory = getCacheDirectory();
final Cache cache = new Cache(cacheDirectory, this.cacheSize);
return new OkHttpConnector(new OkUrlFactory(new OkHttpClient().setCache(cache)));
}
/**
* constructor.
* @param okHttpClient {@link OkHttpClient}
*/
public OkHttpStack(OkHttpClient okHttpClient) {
mOkUrlFactory = new OkUrlFactory(okHttpClient);
}