Last active
March 26, 2019 18:32
-
-
Save alirezaafkar/a62d6a9a7e582322ca1a764bad116a70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OkHttpStack extends BaseHttpStack { | |
public OkHttpStack() { | |
} | |
@Override | |
public HttpResponse executeRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError { | |
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); | |
int timeoutMs = request.getTimeoutMs(); | |
clientBuilder.connectTimeout(timeoutMs, TimeUnit.MILLISECONDS); | |
clientBuilder.readTimeout(timeoutMs, TimeUnit.MILLISECONDS); | |
clientBuilder.writeTimeout(timeoutMs, TimeUnit.MILLISECONDS); | |
okhttp3.Request.Builder okHttpRequestBuilder = new okhttp3.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); | |
OkHttpClient client = clientBuilder.build(); | |
okhttp3.Request okHttpRequest = okHttpRequestBuilder.build(); | |
Call okHttpCall = client.newCall(okHttpRequest); | |
Response okHttpResponse = okHttpCall.execute(); | |
int code = okHttpResponse.code(); | |
ResponseBody body = okHttpResponse.body(); | |
InputStream content = body == null ? null : body.byteStream(); | |
int contentLength = body == null ? 0 : (int) body.contentLength(); | |
List<Header> responseHeaders = mapHeaders(okHttpResponse.headers()); | |
return new HttpResponse(code, responseHeaders, contentLength, content); | |
} | |
private List<Header> mapHeaders(Headers responseHeaders) { | |
List<Header> headers = new ArrayList<>(); | |
for (int i = 0, len = responseHeaders.size(); i < len; i++) { | |
final String name = responseHeaders.name(i), value = responseHeaders.value(i); | |
if (name != null) { | |
headers.add(new Header(name, value)); | |
} | |
} | |
return headers; | |
} | |
private static void setConnectionParametersForRequest(okhttp3.Request.Builder builder, Request<?> request) | |
throws 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.getBody(); | |
if (postBody != null) { | |
builder.post(RequestBody.create(MediaType.parse(request.getBodyContentType()), postBody)); | |
} | |
break; | |
case Request.Method.GET: | |
builder.get(); | |
break; | |
case Request.Method.DELETE: | |
builder.delete(createRequestBody(request)); | |
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 RequestBody createRequestBody(Request r) throws AuthFailureError { | |
final byte[] body = r.getBody(); | |
if (body == null) { | |
return null; | |
} | |
return RequestBody.create(MediaType.parse(r.getBodyContentType()), body); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for the sharing the code! hey, can you update the imports also? with the latest volley Volley 1.1.0
solved: