Skip to content

Instantly share code, notes, and snippets.

@brnhffmnn
Created September 2, 2014 07:54
Show Gist options
  • Save brnhffmnn/8c44d10f16e46a373770 to your computer and use it in GitHub Desktop.
Save brnhffmnn/8c44d10f16e46a373770 to your computer and use it in GitHub Desktop.
Volley + OkHttp
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.SSLSocketFactory;
import com.android.volley.toolbox.HurlStack;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.OkUrlFactory;
public class OkHttpStack extends HurlStack {
private static final String HEADER_USER_AGENT = "User-Agent";
private final String mUserAgent;
private final OkUrlFactory mOkUrlFactory;
public OkHttpStack(String userAgent, UrlRewriter urlRewriter, SSLSocketFactory sslSocketFactory) {
super(urlRewriter);
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setSslSocketFactory(sslSocketFactory);
mOkUrlFactory = new OkUrlFactory(okHttpClient);
mUserAgent = userAgent;
}
@Override
protected HttpURLConnection createConnection(URL url) throws IOException {
HttpURLConnection connection = mOkUrlFactory.open(url);
connection.setRequestProperty(HEADER_USER_AGENT, mUserAgent);
return connection;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment