Instantly share code, notes, and snippets.

Embed
What would you like to do?
A `HttpStack` implementation for Volley that uses OkHttp as its transport.
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);
}
}
@JakeWharton

This comment has been minimized.

Show comment
Hide comment
@JakeWharton
Owner

JakeWharton commented May 21, 2013

@JakeWharton

This comment has been minimized.

Show comment
Hide comment
@JakeWharton

JakeWharton May 22, 2013

Use Volley.newRequestQueue(context, new OkHttpStack()) as of http://r.android.com/59170

Owner

JakeWharton commented May 22, 2013

Use Volley.newRequestQueue(context, new OkHttpStack()) as of http://r.android.com/59170

@shkschneider

This comment has been minimized.

Show comment
Hide comment
@shkschneider

shkschneider Oct 21, 2013

Perfect! Was waiting for something like this since Google I/O 2k13 about Volley.

shkschneider commented Oct 21, 2013

Perfect! Was waiting for something like this since Google I/O 2k13 about Volley.

@tasomaniac

This comment has been minimized.

Show comment
Hide comment
@tasomaniac

tasomaniac Nov 21, 2013

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.

tasomaniac commented Nov 21, 2013

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.

@tadfisher

This comment has been minimized.

Show comment
Hide comment
@tadfisher

tadfisher Dec 21, 2013

@tasomaniac Update Volley from HEAD; they've since re-added the PATCH request method.

tadfisher commented Dec 21, 2013

@tasomaniac Update Volley from HEAD; they've since re-added the PATCH request method.

@peter-tackage

This comment has been minimized.

Show comment
Hide comment
@peter-tackage

peter-tackage Feb 21, 2014

@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 adriancole/retrofit@e704b80 for a reflection based work around and include similar calls in your HurlStack implementation

peter-tackage commented Feb 21, 2014

@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 adriancole/retrofit@e704b80 for a reflection based work around and include similar calls in your HurlStack implementation

@laaptu

This comment has been minimized.

Show comment
Hide comment
@laaptu

laaptu May 2, 2014

@JakeWharton: Is it possible to use HTTPS request with OKHttpClient in conjunction with Volley Library.

laaptu commented May 2, 2014

@JakeWharton: Is it possible to use HTTPS request with OKHttpClient in conjunction with Volley Library.

@zktc5418

This comment has been minimized.

Show comment
Hide comment
@zktc5418

zktc5418 May 20, 2014

@JakeWharton: Is it possible to use HTTPS request with OKHttpClient in conjunction with Volley Library.
i found the same issue. i could not user Https request with OKHttpClient in conjunction with Volley Library.

zktc5418 commented May 20, 2014

@JakeWharton: Is it possible to use HTTPS request with OKHttpClient in conjunction with Volley Library.
i found the same issue. i could not user Https request with OKHttpClient in conjunction with Volley Library.

@s1rius

This comment has been minimized.

Show comment
Hide comment
@s1rius

s1rius May 27, 2014

@JakeWharton: How to use OkHttp 2.0 with Volley ? The "open" method was dismissed.

s1rius commented May 27, 2014

@JakeWharton: How to use OkHttp 2.0 with Volley ? The "open" method was dismissed.

@demonzym

This comment has been minimized.

Show comment
Hide comment
@demonzym

demonzym May 30, 2014

it seems to need com.squareup.okhttp.OkUrlFactory.java, and change the createConnection(URL url) like this:
@override protected HttpURLConnection createConnection(URL url) throws IOException {
return new OkUrlFactory(client).open(url);
}

@JakeWharton @s1rius

maybe differences of new version.

demonzym commented May 30, 2014

it seems to need com.squareup.okhttp.OkUrlFactory.java, and change the createConnection(URL url) like this:
@override protected HttpURLConnection createConnection(URL url) throws IOException {
return new OkUrlFactory(client).open(url);
}

@JakeWharton @s1rius

maybe differences of new version.

@ceram1

This comment has been minimized.

Show comment
Hide comment
@ceram1

ceram1 Jun 16, 2014

I made a new class because I want to use cache from okhttp, not volley.
(Volley does not support syncronous request)

And this is for okhttp 2.0

https://gist.github.com/ceram1/8254f7a68d81172c1669

ceram1 commented Jun 16, 2014

I made a new class because I want to use cache from okhttp, not volley.
(Volley does not support syncronous request)

And this is for okhttp 2.0

https://gist.github.com/ceram1/8254f7a68d81172c1669

@zapotec83

This comment has been minimized.

Show comment
Hide comment
@zapotec83

zapotec83 Jun 24, 2014

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?

zapotec83 commented Jun 24, 2014

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?

@imminent

This comment has been minimized.

Show comment
Hide comment
@imminent

imminent Jul 7, 2014

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 open was relocated to the OkUrlFactory (which is part of okhttp-urlconnection)

imminent commented Jul 7, 2014

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 open was relocated to the OkUrlFactory (which is part of okhttp-urlconnection)

@DeepShah9

This comment has been minimized.

Show comment
Hide comment
@DeepShah9

DeepShah9 Jul 8, 2014

@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)

DeepShah9 commented Jul 8, 2014

@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)

@quqiufeng

This comment has been minimized.

Show comment
Hide comment
@quqiufeng

quqiufeng Jul 14, 2014

how can i get the jar file with this file added in?

quqiufeng commented Jul 14, 2014

how can i get the jar file with this file added in?

@nachtien

This comment has been minimized.

Show comment
Hide comment
@nachtien

nachtien Sep 10, 2014

To get @imminent's code to work, you need to add

compile 'com.squareup.okhttp:okhttp-urlconnection:1.6.+'

to your build.gradle

nachtien commented Sep 10, 2014

To get @imminent's code to work, you need to add

compile 'com.squareup.okhttp:okhttp-urlconnection:1.6.+'

to your build.gradle

@hpadrao

This comment has been minimized.

Show comment
Hide comment
@hpadrao

hpadrao Nov 27, 2014

Does it make sense? (In order to avoid "OkHttp changes the global SSL context, breaks other HTTP clients" square/okhttp#184 )

/**
 * 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.");
        }

        try {
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, null, null);
            client.setSslSocketFactory(sslContext.getSocketFactory());
        } catch (Exception e) {
            throw new AssertionError(); // The system has no TLS. Just give up.
        }

        mFactory = new OkUrlFactory(client);
    }

    @Override
    protected HttpURLConnection createConnection(URL url) throws IOException {
        return mFactory.open(url);
    }
}

hpadrao commented Nov 27, 2014

Does it make sense? (In order to avoid "OkHttp changes the global SSL context, breaks other HTTP clients" square/okhttp#184 )

/**
 * 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.");
        }

        try {
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, null, null);
            client.setSslSocketFactory(sslContext.getSocketFactory());
        } catch (Exception e) {
            throw new AssertionError(); // The system has no TLS. Just give up.
        }

        mFactory = new OkUrlFactory(client);
    }

    @Override
    protected HttpURLConnection createConnection(URL url) throws IOException {
        return mFactory.open(url);
    }
}
@mehmet6parmak

This comment has been minimized.

Show comment
Hide comment
@mehmet6parmak

mehmet6parmak Dec 25, 2014

Neither OkHttpClient nor OkUrlFactory has a method named open now. None of the above implementations work with the latest versions of OkHttpClient.

mehmet6parmak commented Dec 25, 2014

Neither OkHttpClient nor OkUrlFactory has a method named open now. None of the above implementations work with the latest versions of OkHttpClient.

@toanpv

This comment has been minimized.

Show comment
Hide comment

toanpv commented Jan 5, 2015

@pdichone

This comment has been minimized.

Show comment
Hide comment
@pdichone

pdichone Jan 24, 2015

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:

  • All I am trying to do is to be able to us PATCH to send data over to a server. I haven't been able to do that with Volley for the past 2 days... and I am pretty much hitting my head into the wall right now :(

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:

java.net.ProtocolException: Unknown method 'PATCH'; must be one of [OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE] which is something that one other person got too. However, I don't understand how to solve this problem.

Can you please help?

Thanks

pdichone commented Jan 24, 2015

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:

  • All I am trying to do is to be able to us PATCH to send data over to a server. I haven't been able to do that with Volley for the past 2 days... and I am pretty much hitting my head into the wall right now :(

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:

java.net.ProtocolException: Unknown method 'PATCH'; must be one of [OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE] which is something that one other person got too. However, I don't understand how to solve this problem.

Can you please help?

Thanks

@JeffMcKnight

This comment has been minimized.

Show comment
Hide comment
@JeffMcKnight

JeffMcKnight Mar 24, 2015

This implementation of OkHttpStack works with OkHttpClient v2.3.0 (doesn't use open(url) method):
https://gist.github.com/bryanstern/4e8f1cb5a8e14c202750

JeffMcKnight commented Mar 24, 2015

This implementation of OkHttpStack works with OkHttpClient v2.3.0 (doesn't use open(url) method):
https://gist.github.com/bryanstern/4e8f1cb5a8e14c202750

@vfede

This comment has been minimized.

Show comment
Hide comment
@vfede

vfede Jul 2, 2015

@imminent version still works with these dependencies in gradle:

compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.4.0'

then you just have to init your RequestQueue with:

myRequestQueue = Volley.newRequestQueue(context, new OkHttpStack())

vfede commented Jul 2, 2015

@imminent version still works with these dependencies in gradle:

compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.4.0'

then you just have to init your RequestQueue with:

myRequestQueue = Volley.newRequestQueue(context, new OkHttpStack())
@GleidsonFerSanP

This comment has been minimized.

Show comment
Hide comment
@GleidsonFerSanP

GleidsonFerSanP commented Dec 11, 2015

@JakeWharton i' m your fan!!

@astuter

This comment has been minimized.

Show comment
Hide comment
@astuter

astuter Sep 9, 2016

Any update on how we can use Volley with okhttp3 ?

astuter commented Sep 9, 2016

Any update on how we can use Volley with okhttp3 ?

@Anawaz

This comment has been minimized.

Show comment
Hide comment
@Anawaz

Anawaz Sep 10, 2016

Any update on how we can use Volley with okhttp3 ?

Anawaz commented Sep 10, 2016

Any update on how we can use Volley with okhttp3 ?

@SylvainHocq

This comment has been minimized.

Show comment
Hide comment
@sushant4anshu

This comment has been minimized.

Show comment
Hide comment
@sushant4anshu

sushant4anshu Jun 27, 2017

client.open(url);

open method is not working,
Is there any working alternative for this?.

sushant4anshu commented Jun 27, 2017

client.open(url);

open method is not working,
Is there any working alternative for this?.

@TamirSagiGimso

This comment has been minimized.

Show comment
Hide comment
@TamirSagiGimso

TamirSagiGimso Mar 13, 2018

@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());

TamirSagiGimso commented Mar 13, 2018

@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());

@khat33b

This comment has been minimized.

Show comment
Hide comment
@khat33b

khat33b May 24, 2018

What is the advantage of using OkHttp as transport for Volley?

khat33b commented May 24, 2018

What is the advantage of using OkHttp as transport for Volley?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment