okhttp用法有错误;添加了枚举HttpType (#207)

This commit is contained in:
crskyp
2017-05-04 11:13:02 +08:00
committed by Binary Wang
parent 424337a7a7
commit fbd02a85c9
21 changed files with 1182 additions and 37 deletions

View File

@ -17,26 +17,29 @@ import okhttp3.ConnectionPool;
public abstract class AbstractRequestExecutor<T, E> implements RequestExecutor<T, E> {
@Override
public T execute(RequestHttp requestHttp, String uri, E data) throws WxErrorException, IOException{
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
//apache-http请求
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
return executeApache(httpClient, httpProxy, uri, data);
}
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
//jodd-http请求
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
return executeJodd(provider, proxyInfo, uri, data);
} else if (requestHttp.getRequestHttpClient() instanceof ConnectionPool) {
//okhttp请求
ConnectionPool pool = (ConnectionPool) requestHttp.getRequestHttpClient();
OkhttpProxyInfo proxyInfo = (OkhttpProxyInfo) requestHttp.getRequestHttpProxy();
return executeOkhttp(pool, proxyInfo, uri, data);
} else {
//TODO 这里需要抛出异常,需要优化
return null;
public T execute(RequestHttp requestHttp, String uri, E data) throws WxErrorException, IOException {
switch (requestHttp.getRequestType()) {
case apacheHttp: {
//apache-http请求
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
return executeApache(httpClient, httpProxy, uri, data);
}
case joddHttp: {
//jodd-http请求
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
return executeJodd(provider, proxyInfo, uri, data);
}
case okHttp: {
//okhttp请求
ConnectionPool pool = (ConnectionPool) requestHttp.getRequestHttpClient();
OkhttpProxyInfo proxyInfo = (OkhttpProxyInfo) requestHttp.getRequestHttpProxy();
return executeOkhttp(pool, proxyInfo, uri, data);
}
default:
//TODO 这里需要抛出异常,需要优化
return null;
}
}

View File

@ -0,0 +1,8 @@
package me.chanjar.weixin.common.util.http;
/**
* Created by ecoolper on 2017/4/28.
*/
public enum HttpType {
joddHttp, apacheHttp, okHttp;
}

View File

@ -213,7 +213,7 @@ public class MediaDownloadRequestExecutor extends AbstractRequestExecutor<File,
String contentType = response.header("Content-Type");
if (contentType != null && contentType.startsWith("application/json")) {
// application/json; encoding=utf-8 下载媒体文件出错
throw new WxErrorException(WxError.fromJson(response.body().toString()));
throw new WxErrorException(WxError.fromJson(response.body().string()));
}
String fileName = getFileName(response);

View File

@ -136,7 +136,7 @@ public class MediaUploadRequestExecutor extends AbstractRequestExecutor<WxMediaU
Request request = new Request.Builder().url(uri).post(body).build();
Response response = client.newCall(request).execute();
String responseContent = response.body().toString();
String responseContent = response.body().string();
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);

View File

@ -17,4 +17,10 @@ public interface RequestHttp<H,P> {
*/
P getRequestHttpProxy();
/**
*
* @return
*/
HttpType getRequestType();
}

View File

@ -141,7 +141,7 @@ public class SimpleGetRequestExecutor extends AbstractRequestExecutor<String, St
Request request = new Request.Builder().url(uri).build();
Response response = client.newCall(request).execute();
String responseContent = response.body().toString();
String responseContent = response.body().string();
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);

View File

@ -156,7 +156,7 @@ public class SimplePostRequestExecutor extends AbstractRequestExecutor<String, S
Request request = new Request.Builder().url(uri).post(body).build();
Response response = client.newCall(request).execute();
String responseContent = response.body().toString();
String responseContent = response.body().string();
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);