-
-
Save JakeWharton/5616899 to your computer and use it in GitHub Desktop.
import com.android.volley.toolbox.HurlStack; | |
import com.squareup.okhttp.OkHttpClient; | |
import java.io.IOException; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
/** | |
* 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; | |
} | |
@Override protected HttpURLConnection createConnection(URL url) throws IOException { | |
return client.open(url); | |
} | |
} |
@sushant4anshu, yes,
That one works for me
add these libraries to gradle file
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:1.6.+'
then :
/**
* An {@link com.android.volley.toolbox.HttpStack HttpStack} implementation which
* uses OkHttp as its transport.
*/
private 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.");
}
mFactory = new OkUrlFactory(client);
}
@Override protected HttpURLConnection createConnection(URL url) throws IOException {
return mFactory.open(url);
}
}
while creating Volley Queue:
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext(), new OkHttpStack());
What is the advantage of using OkHttp
as transport for Volley
?
OkUrlFactory is gone
It was deprecated in December 2015 and deleted in March 2019. You should use this instead:
https://gist.github.com/bryanstern/4e8f1cb5a8e14c202750
thank you guys for the insight, here the updated version using volley 1.2.1 (latest) https://gist.github.com/4sskick/d1318379500747564d8f1c8d6af1f796 in case still you guys still has old project which need to maintain but don't want to refactor to OkHttp or another request library
client.open(url);
open method is not working,
Is there any working alternative for this?.