Skip to content

Instantly share code, notes, and snippets.

@tbruyelle
Created August 7, 2014 08:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbruyelle/0729aef4df2c11b21fdf to your computer and use it in GitHub Desktop.
Save tbruyelle/0729aef4df2c11b21fdf to your computer and use it in GitHub Desktop.
Volley & OkHttp
package com.comalia.gesicamobile.manager.net;
import com.android.volley.toolbox.HurlStack;
import com.comalia.gesicamobile.manager.util.NukeSSLCerts;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.OkUrlFactory;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
/**
* An {@link com.android.volley.toolbox.HttpStack HttpStack} implementation which
* uses OkHttp as its transport.
*/
public class OkHttpStack extends HurlStack {
private final OkUrlFactory mFactory;
public OkHttpStack() {
this(new OkHttpClient());
}
public OkHttpStack(OkHttpClient client) {
if (client == null) {
throw new NullPointerException("Client must not be null.");
}
client.setSslSocketFactory(NukeSSLCerts.getSSLSocketFactory());
client.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
mFactory = new OkUrlFactory(client);
}
@Override
protected HttpURLConnection createConnection(URL url) throws IOException {
return mFactory.open(url);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment