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); | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
Use |
This comment has been minimized.
This comment has been minimized.
Perfect! Was waiting for something like this since Google I/O 2k13 about Volley. |
This comment has been minimized.
This comment has been minimized.
I am getting "Unknown method 'PATCH'; must be one of [OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE]" when using Volley with OKHttp. What do I do? I have tried everything. |
This comment has been minimized.
This comment has been minimized.
@tasomaniac Update Volley from HEAD; they've since re-added the PATCH request method. |
This comment has been minimized.
This comment has been minimized.
@tasomaniac that ProtocolException is being thrown by base HttpUrlConnection class which doesn't support PATCH in its getRequestMethod() (the list of the supported Methods is in a private array) You should check out https://github.com/adriancole/retrofit/commit/e704b800878b2e37f5ac98b0139cb4994618ace0 for a reflection based work around and include similar calls in your HurlStack implementation |
This comment has been minimized.
This comment has been minimized.
@JakeWharton: Is it possible to use HTTPS request with OKHttpClient in conjunction with Volley Library. |
This comment has been minimized.
This comment has been minimized.
@JakeWharton: Is it possible to use HTTPS request with OKHttpClient in conjunction with Volley Library. |
This comment has been minimized.
This comment has been minimized.
@JakeWharton: How to use OkHttp 2.0 with Volley ? The "open" method was dismissed. |
This comment has been minimized.
This comment has been minimized.
it seems to need com.squareup.okhttp.OkUrlFactory.java, and change the createConnection(URL url) like this: maybe differences of new version. |
This comment has been minimized.
This comment has been minimized.
I made a new class because I want to use cache from okhttp, not volley. And this is for okhttp 2.0 |
This comment has been minimized.
This comment has been minimized.
How can I use Volley and OkHttp 2.0 to accept my self signed Certificate? I am begggining to use OkHttp 2.0 alone inside an Async Task and no problems, but I don't know how to make it work with Volley....Anybody knows? |
This comment has been minimized.
This comment has been minimized.
With the most recent OkHttp I had to change to: import com.android.volley.toolbox.HurlStack;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.OkUrlFactory;
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 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);
}
} Since |
This comment has been minimized.
This comment has been minimized.
@imminent , it's nice you have implement OkHttp With Volley but i want to know how to use this Custom OkHttpStack class that you have implement ? (i am Newbie) |
This comment has been minimized.
This comment has been minimized.
how can i get the jar file with this file added in? |
This comment has been minimized.
This comment has been minimized.
To get @imminent's code to work, you need to add
to your build.gradle |
This comment has been minimized.
This comment has been minimized.
Does it make sense? (In order to avoid "OkHttp changes the global SSL context, breaks other HTTP clients" square/okhttp#184 )
|
This comment has been minimized.
This comment has been minimized.
Neither OkHttpClient nor OkUrlFactory has a method named open now. None of the above implementations work with the latest versions of OkHttpClient. |
This comment has been minimized.
This comment has been minimized.
@mehmet6parmak |
This comment has been minimized.
This comment has been minimized.
I am sorry guys, but how do I add the url connection you mentioned? I am using the latest Android Studio IDE. I guess I should explain what I am trying to do:
I followed previous comments on how to use OkHttpStack implementation. I did all that but I am getting this error when I run my app in the emulator:
Can you please help? Thanks |
This comment has been minimized.
This comment has been minimized.
This implementation of OkHttpStack works with OkHttpClient v2.3.0 (doesn't use open(url) method): |
This comment has been minimized.
This comment has been minimized.
@imminent version still works with these dependencies in gradle:
then you just have to init your RequestQueue with:
|
This comment has been minimized.
This comment has been minimized.
@JakeWharton i' m your fan!! |
This comment has been minimized.
This comment has been minimized.
Any update on how we can use Volley with okhttp3 ? |
This comment has been minimized.
This comment has been minimized.
Any update on how we can use Volley with okhttp3 ? |
This comment has been minimized.
This comment has been minimized.
Yep! you can look here : https://gist.github.com/SylvainHocq/36d6b17e53c7004f6ae59e689ec0862f |
This comment has been minimized.
This comment has been minimized.
client.open(url); open method is not working, |
This comment has been minimized.
This comment has been minimized.
@sushant4anshu, yes, add these libraries to gradle file then : /**
while creating Volley Queue: |
This comment has been minimized.
This comment has been minimized.
What is the advantage of using |
This comment has been minimized.
This comment has been minimized.
OkUrlFactory is goneIt was deprecated in December 2015 and deleted in March 2019. You should use this instead: |
This comment has been minimized.
Depends on http://r.android.com/59071