Created
September 2, 2014 07:54
-
-
Save brnhffmnn/8c44d10f16e46a373770 to your computer and use it in GitHub Desktop.
Volley + OkHttp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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