Instantly share code, notes, and snippets.

Embed
What would you like to do?
An OkHttp backed HttpStack for Volley
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Circle Internet Financial
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.circle.android.api;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.toolbox.HttpStack;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Headers;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Protocol;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.StatusLine;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicStatusLine;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* OkHttp backed {@link com.android.volley.toolbox.HttpStack HttpStack} that does not
* use okhttp-urlconnection
*/
public class OkHttpStack implements HttpStack {
private final OkHttpClient mClient;
public OkHttpStack(OkHttpClient client) {
this.mClient = client;
}
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
OkHttpClient client = mClient.clone();
int timeoutMs = request.getTimeoutMs();
client.setConnectTimeout(timeoutMs, TimeUnit.MILLISECONDS);
client.setReadTimeout(timeoutMs, TimeUnit.MILLISECONDS);
client.setWriteTimeout(timeoutMs, TimeUnit.MILLISECONDS);
com.squareup.okhttp.Request.Builder okHttpRequestBuilder = new com.squareup.okhttp.Request.Builder();
okHttpRequestBuilder.url(request.getUrl());
Map<String, String> headers = request.getHeaders();
for (final String name : headers.keySet()) {
okHttpRequestBuilder.addHeader(name, headers.get(name));
}
for (final String name : additionalHeaders.keySet()) {
okHttpRequestBuilder.addHeader(name, additionalHeaders.get(name));
}
setConnectionParametersForRequest(okHttpRequestBuilder, request);
com.squareup.okhttp.Request okHttpRequest = okHttpRequestBuilder.build();
Call okHttpCall = client.newCall(okHttpRequest);
Response okHttpResponse = okHttpCall.execute();
StatusLine responseStatus = new BasicStatusLine(parseProtocol(okHttpResponse.protocol()), okHttpResponse.code(), okHttpResponse.message());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
response.setEntity(entityFromOkHttpResponse(okHttpResponse));
Headers responseHeaders = okHttpResponse.headers();
for (int i = 0, len = responseHeaders.size(); i < len; i++) {
final String name = responseHeaders.name(i), value = responseHeaders.value(i);
if (name != null) {
response.addHeader(new BasicHeader(name, value));
}
}
return response;
}
private static HttpEntity entityFromOkHttpResponse(Response r) throws IOException {
BasicHttpEntity entity = new BasicHttpEntity();
ResponseBody body = r.body();
entity.setContent(body.byteStream());
entity.setContentLength(body.contentLength());
entity.setContentEncoding(r.header("Content-Encoding"));
if (body.contentType() != null) {
entity.setContentType(body.contentType().type());
}
return entity;
}
@SuppressWarnings("deprecation")
private static void setConnectionParametersForRequest(com.squareup.okhttp.Request.Builder builder, Request<?> request)
throws IOException, AuthFailureError {
switch (request.getMethod()) {
case Request.Method.DEPRECATED_GET_OR_POST:
// Ensure backwards compatibility. Volley assumes a request with a null body is a GET.
byte[] postBody = request.getPostBody();
if (postBody != null) {
builder.post(RequestBody.create(MediaType.parse(request.getPostBodyContentType()), postBody));
}
break;
case Request.Method.GET:
builder.get();
break;
case Request.Method.DELETE:
builder.delete();
break;
case Request.Method.POST:
builder.post(createRequestBody(request));
break;
case Request.Method.PUT:
builder.put(createRequestBody(request));
break;
case Request.Method.HEAD:
builder.head();
break;
case Request.Method.OPTIONS:
builder.method("OPTIONS", null);
break;
case Request.Method.TRACE:
builder.method("TRACE", null);
break;
case Request.Method.PATCH:
builder.patch(createRequestBody(request));
break;
default:
throw new IllegalStateException("Unknown method type.");
}
}
private static ProtocolVersion parseProtocol(final Protocol p) {
switch (p) {
case HTTP_1_0:
return new ProtocolVersion("HTTP", 1, 0);
case HTTP_1_1:
return new ProtocolVersion("HTTP", 1, 1);
case SPDY_3:
return new ProtocolVersion("SPDY", 3, 1);
case HTTP_2:
return new ProtocolVersion("HTTP", 2, 0);
}
throw new IllegalAccessError("Unkwown protocol");
}
private static RequestBody createRequestBody(Request r) throws AuthFailureError {
final byte[] body = r.getBody();
if (body == null) return null;
return RequestBody.create(MediaType.parse(r.getBodyContentType()), body);
}
}
@ReneeVandervelde

This comment has been minimized.

ReneeVandervelde commented Feb 21, 2015

Holy NullPointer batman!
Got a little trigger-happy with that Content-type object. Best check if that's null before we try calling type() on line #114

     Caused by: java.lang.NullPointerException
            at com.circle.android.api.OkHttpStack.entityFromOkHttpResponse(OkHttpStack.java:114)
            at com.circle.android.api.OkHttpStack.performRequest(OkHttpStack.java:93)
            at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
            at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:110)
@bryanstern

This comment has been minimized.

Owner

bryanstern commented Feb 22, 2015

Good catch @MaxVandervelde! Revised

@buddhisthead

This comment has been minimized.

buddhisthead commented May 5, 2015

Very nice. Does this implementation support the interceptors feature? I came looking because the OkUrlFactory does not. Thanks.

@Brian-McBride

This comment has been minimized.

Brian-McBride commented May 6, 2015

My post body isn't getting passed through your code.

@Override
public byte[] getBody() throws AuthFailureError {
    String object = mGson.toJson(body);
    return object.getBytes();
@dbachelder

This comment has been minimized.

dbachelder commented May 6, 2015

typo line 172: Unkwown -> Unknown

@Sottti

This comment has been minimized.

Sottti commented Jul 2, 2015

Some deprecated stuff on this implementation as the apache client has been deprecated on Lollipop, and looks like won't be included on Android M. Any thoughts on that?

@GautamGupta

This comment has been minimized.

GautamGupta commented Jul 23, 2015

Thanks!

@funkyidol

This comment has been minimized.

funkyidol commented Aug 15, 2015

Agree with @Scottti. Looking for a newer implementation to replace the deprecated apache libraries

@Sottti

This comment has been minimized.

Sottti commented Aug 18, 2015

This is not working anymore as of targeting SDK 23. Apache is not in anymore.

Any alternatives?

@ninchuu

This comment has been minimized.

ninchuu commented Aug 21, 2015

Hey Sottti, as a temporary workaround I managed to get it compiling by including httpcore-4.4.1.jar (http://hc.apache.org/downloads.cgi). Stetho seems to work fine after that.

@Sottti

This comment has been minimized.

Sottti commented Sep 13, 2015

@ninchuu at the moment is easier go with this in you gradle module file:

useLibrary 'org.apache.http.legacy'

https://goo.gl/KSc3TF

@rvijayraghavan

This comment has been minimized.

rvijayraghavan commented Sep 17, 2015

@Sottti Yeah, looks like that's the way to go about it for now until an update is released.

@alashow

This comment has been minimized.

alashow commented Jan 13, 2016

@AliMehrpour

This comment has been minimized.

AliMehrpour commented Feb 4, 2016

Can anybody explain what is the benefit of using OkHttp as HttpStack for Volley library?

@alashow

This comment has been minimized.

alashow commented Feb 28, 2016

@AliMehrpour "faster".

@amadib

This comment has been minimized.

amadib commented May 8, 2016

Considering Apache HTTP Client Removal, how would our HttpStack look without having useLibrary 'org.apache.http.legacy'?

@ysemylord

This comment has been minimized.

ysemylord commented Jun 1, 2016

in line 141 I think should add judging conditions like thik:
if(requestBody!=null) {
builder.post(requestBody);
}

@Ahmed-Mostafa-Ghanayem

This comment has been minimized.

Ahmed-Mostafa-Ghanayem commented Nov 23, 2016

Line 126 method getPostBody is deprecated
use getBody instead

Line 128 method getPostBodyContentType is deprecated
use getBodyContentType instead

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