Skip to content

Instantly share code, notes, and snippets.

@Jaymo
Last active August 29, 2015 14:02
Show Gist options
  • Save Jaymo/e13c939516f73d18a95e to your computer and use it in GitHub Desktop.
Save Jaymo/e13c939516f73d18a95e to your computer and use it in GitHub Desktop.
A `HttpStack` implementation for Volley that uses OkHttp 2.0 as its transport.
package com.ujuzicode.repocode;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import com.android.volley.toolbox.HurlStack;
import com.squareup.okhttp.OkHttpClient;
/**
* An {@link com.android.volley.toolbox.HttpStack HttpStack} implementation which
* uses OkHttp as its transport.
*/
public class OkHttpStack extends HurlStack {
private final OkHttpClient client;
public OkHttpStack() {
this(new OkHttpClient());
}
public OkHttpStack(OkHttpClient client) {
if (client == null) {
throw new NullPointerException("Client must not be null.");
}
this.client = client;
}
// The below is unusable as of OkHTTP 2.0
/**@Override protected HttpURLConnection createConnection(URL url) throws IOException {
return client.open(url);
} */
@Override protected HttpURLConnection createConnection(URL url) throws IOException {
return new OkUrlFactory(client).open(url);
}
}
import com.squareup.okhttp.internal.InternalCache;
import com.squareup.okhttp.internal.huc.CacheAdapter;
import com.squareup.okhttp.internal.huc.HttpURLConnectionImpl;
import com.squareup.okhttp.internal.huc.HttpsURLConnectionImpl;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.ResponseCache;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;
public final class OkUrlFactory implements URLStreamHandlerFactory, Cloneable {
private final OkHttpClient client;
public OkUrlFactory(OkHttpClient client) {
this.client = client;
}
public OkHttpClient client() {
return client;
}
/** Sets the response cache to be used to read and write cached responses. */
OkUrlFactory setResponseCache(ResponseCache responseCache) {
client.setInternalCache(responseCache != null ? new CacheAdapter(responseCache) : null);
return this;
}
ResponseCache getResponseCache() {
InternalCache cache = client.internalCache();
return cache instanceof CacheAdapter ? ((CacheAdapter) cache).getDelegate() : null;
}
/**
* Returns a copy of this stream handler factory that includes a shallow copy
* of the internal {@linkplain OkHttpClient HTTP client}.
*/
@Override public OkUrlFactory clone() {
return new OkUrlFactory(client.clone());
}
public HttpURLConnection open(URL url) {
return open(url, client.getProxy());
}
HttpURLConnection open(URL url, Proxy proxy) {
String protocol = url.getProtocol();
OkHttpClient copy = client.copyWithDefaults();
copy.setProxy(proxy);
if (protocol.equals("http")) return new HttpURLConnectionImpl(url, copy);
if (protocol.equals("https")) return new HttpsURLConnectionImpl(url, copy);
throw new IllegalArgumentException("Unexpected protocol: " + protocol);
}
/**
* Creates a URLStreamHandler as a {@link java.net.URL#setURLStreamHandlerFactory}.
*
* <p>This code configures OkHttp to handle all HTTP and HTTPS connections
* created with {@link java.net.URL#openConnection()}: <pre> {@code
*
* OkHttpClient okHttpClient = new OkHttpClient();
* URL.setURLStreamHandlerFactory(new OkUrlFactory(okHttpClient));
* }</pre>
*/
@Override public URLStreamHandler createURLStreamHandler(final String protocol) {
if (!protocol.equals("http") && !protocol.equals("https")) return null;
return new URLStreamHandler() {
@Override protected URLConnection openConnection(URL url) {
return open(url);
}
@Override protected URLConnection openConnection(URL url, Proxy proxy) {
return open(url, proxy);
}
@Override protected int getDefaultPort() {
if (protocol.equals("http")) return 80;
if (protocol.equals("https")) return 443;
throw new AssertionError();
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment