Created
April 24, 2018 10:36
-
-
Save lzh77/616b480296234ea8ce4ec07d02f281e4 to your computer and use it in GitHub Desktop.
simple volley httpstack impl with okhttp3.
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
package com.janus.newstudyproject.http; | |
import com.android.volley.AuthFailureError; | |
import com.android.volley.Request; | |
import com.android.volley.toolbox.HttpStack; | |
import org.apache.http.Header; | |
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.net.URL; | |
import java.util.Map; | |
import java.util.concurrent.TimeUnit; | |
import javax.net.ssl.SSLSocketFactory; | |
import okhttp3.Call; | |
import okhttp3.Headers; | |
import okhttp3.MediaType; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Protocol; | |
import okhttp3.RequestBody; | |
import okhttp3.Response; | |
import okhttp3.ResponseBody; | |
/** | |
* Created by admin on 2018/4/24. | |
*/ | |
public class OkHttpStack implements HttpStack { | |
private final SSLSocketFactory mSslSocketFactory; | |
public OkHttpStack() { | |
this(null); | |
} | |
public OkHttpStack(SSLSocketFactory sslSocketFactory) { | |
this.mSslSocketFactory = sslSocketFactory; | |
} | |
@Override | |
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) | |
throws IOException, AuthFailureError { | |
int timeout = request.getTimeoutMs(); | |
String url = request.getUrl(); | |
//build client | |
OkHttpClient.Builder okBuilder = new OkHttpClient.Builder(); | |
okBuilder.connectTimeout(timeout, TimeUnit.MILLISECONDS); | |
okBuilder.readTimeout(timeout, TimeUnit.MILLISECONDS); | |
okBuilder.writeTimeout(timeout, TimeUnit.MILLISECONDS); | |
if ("https".equals(new URL(url).getProtocol()) && mSslSocketFactory != null) { | |
okBuilder.sslSocketFactory(mSslSocketFactory); | |
} | |
OkHttpClient client = okBuilder.build(); | |
//build request | |
okhttp3.Request.Builder okRequestBuilder = new okhttp3.Request.Builder(); | |
okRequestBuilder.url(url); | |
Map<String, String> headers = request.getHeaders(); | |
for (final String name : headers.keySet()) { | |
okRequestBuilder.addHeader(name, headers.get(name)); | |
} | |
for (final String name : additionalHeaders.keySet()) { | |
okRequestBuilder.addHeader(name, additionalHeaders.get(name)); | |
} | |
setConnectionParametersForRequest(okRequestBuilder, request); | |
okhttp3.Request okRequest = okRequestBuilder.build(); | |
//call | |
Call okCall = client.newCall(okRequest); | |
//build response | |
Response okResponse = okCall.execute(); | |
StatusLine responseStatus = new BasicStatusLine(parseProtocol(okResponse.protocol()), | |
okResponse.code(), okResponse.message()); | |
BasicHttpResponse response = new BasicHttpResponse(responseStatus); | |
response.setEntity(entityFromOkHttpResponse(okResponse)); | |
Headers responseHeaders = okResponse.headers(); | |
for (int i = 0, len = responseHeaders.size(); i < len; i++) { | |
final String name = responseHeaders.name(i), value = responseHeaders.value(i); | |
if (name != null) { | |
Header header = new BasicHeader(name, value); | |
response.addHeader(header); | |
} | |
} | |
return response; | |
} | |
private HttpEntity entityFromOkHttpResponse(Response response) throws IOException { | |
BasicHttpEntity entity = new BasicHttpEntity(); | |
ResponseBody body = response.body(); | |
entity.setContent(body.byteStream()); | |
entity.setContentLength(body.contentLength()); | |
entity.setContentEncoding(response.header("Content-Encoding")); | |
if (body.contentType() != null) { | |
entity.setContentType(body.contentType().type()); | |
} | |
return entity; | |
} | |
private void setConnectionParametersForRequest(okhttp3.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 ProtocolVersion parseProtocol(Protocol protocol) { | |
switch (protocol) { | |
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 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