Skip to content

Instantly share code, notes, and snippets.

@showsky
Created May 27, 2014 07:08
Show Gist options
  • Select an option

  • Save showsky/3f7e5958ff1f8d2bc5df to your computer and use it in GitHub Desktop.

Select an option

Save showsky/3f7e5958ff1f8d2bc5df to your computer and use it in GitHub Desktop.
Volley network core use OkHttp
public class OKStack implements HttpStack {
OkHttpClient client = new OkHttpClient();
public static final String HEADER_CONTENT_TYPE = "Content-Type";
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException,
AuthFailureError {
URL url = new URL(request.getUrl());
client.setReadTimeout(request.getTimeoutMs(), TimeUnit.MILLISECONDS);
client.setConnectTimeout(request.getTimeoutMs(), TimeUnit.MILLISECONDS);
HttpURLConnection conn = client.open(url);
conn.setUseCaches(false);
conn.setDoInput(true);
Map<String, String> params = new HashMap<String, String>();
params.putAll(additionalHeaders);
params.putAll(request.getHeaders());
for (Entry<String, String> entry : params.entrySet())
conn.addRequestProperty(entry.getKey(), entry.getValue());
setRequestMethod(conn, request);
ProtocolVersion version = new ProtocolVersion("http", 1, 1);
int code = conn.getResponseCode();
if (code == -1) {
throw new IOException("Could not retrieve response code from Connection");
}
StatusLine state = new BasicStatusLine(version, code, conn.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(state);
response.setEntity(buildResponseEntity(conn));
for (Entry<String, List<String>> item : conn.getHeaderFields().entrySet()) {
if (item.getKey() != null) {
Header head = new BasicHeader(item.getKey(), item.getValue().get(0));
response.setHeader(head);
}
}
return response;
}
HttpEntity buildResponseEntity(HttpURLConnection conn) {
BasicHttpEntity entity = new BasicHttpEntity();
InputStream is;
try {
is = conn.getInputStream();
} catch (IOException e) {
is = conn.getErrorStream();
}
entity.setContent(is);
entity.setContentEncoding(conn.getContentEncoding());
entity.setContentLength(conn.getContentLength());
entity.setContentType(conn.getContentType());
return entity;
}
void addBody(HttpURLConnection conn, Request<?> request) throws AuthFailureError, IOException {
byte[] body = request.getBody();
if (body != null) {
conn.setDoOutput(true);
conn.addRequestProperty(HEADER_CONTENT_TYPE, request.getBodyContentType());
DataOutputStream os = new DataOutputStream(conn.getOutputStream());
os.write(body);
os.close();
}
}
void setRequestMethod(HttpURLConnection conn, Request<?> request) throws AuthFailureError, IOException {
switch (request.getMethod()) {
case Method.DEPRECATED_GET_OR_POST:
byte[] body = request.getBody();
if (body != null) {
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.addRequestProperty(HEADER_CONTENT_TYPE, request.getBodyContentType());
DataOutputStream os = new DataOutputStream(conn.getOutputStream());
os.write(body);
os.close();
}
break;
case Method.POST:
conn.setRequestMethod("POST");
addBody(conn, request);
break;
case Method.PUT:
conn.setRequestMethod("PUT");
addBody(conn, request);
break;
case Method.GET:
conn.setRequestMethod("GET");
break;
case Method.DELETE:
conn.setRequestMethod("DELETE");
break;
default:
throw new IllegalStateException("Unkonw Request Method.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment