Skip to content

Instantly share code, notes, and snippets.

@JakeWharton
Created May 21, 2013 01:14
Show Gist options
  • Save JakeWharton/5616899 to your computer and use it in GitHub Desktop.
Save JakeWharton/5616899 to your computer and use it in GitHub Desktop.
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);
}
}
@hpadrao
Copy link

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
Copy link

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

@toanpv
Copy link

toanpv commented Jan 5, 2015

@pdichone
Copy link

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
Copy link

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

@vfede
Copy link

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
Copy link

@JakeWharton i' m your fan!!

@uniruddh
Copy link

uniruddh commented Sep 9, 2016

Any update on how we can use Volley with okhttp3 ?

@Anawaz
Copy link

Anawaz commented Sep 10, 2016

Any update on how we can use Volley with okhttp3 ?

@SylvainHocq
Copy link

@sushant4anshu
Copy link

client.open(url);

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

@TamirSagiGimso
Copy link

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
Copy link

khat33b commented May 24, 2018

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

@swankjesse
Copy link

swankjesse commented Mar 2, 2019

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

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