Skip to content

Commit fbd02a8

Browse files
crskypbinarywang
authored andcommitted
okhttp用法有错误;添加了枚举HttpType (binarywang#207)
1 parent 424337a commit fbd02a8

21 files changed

+1182
-37
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/AbstractRequestExecutor.java

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,29 @@
1717
public abstract class AbstractRequestExecutor<T, E> implements RequestExecutor<T, E> {
1818

1919
@Override
20-
public T execute(RequestHttp requestHttp, String uri, E data) throws WxErrorException, IOException{
21-
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
22-
//apache-http请求
23-
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
24-
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
25-
return executeApache(httpClient, httpProxy, uri, data);
26-
}
27-
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
28-
//jodd-http请求
29-
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
30-
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
31-
return executeJodd(provider, proxyInfo, uri, data);
32-
} else if (requestHttp.getRequestHttpClient() instanceof ConnectionPool) {
33-
//okhttp请求
34-
ConnectionPool pool = (ConnectionPool) requestHttp.getRequestHttpClient();
35-
OkhttpProxyInfo proxyInfo = (OkhttpProxyInfo) requestHttp.getRequestHttpProxy();
36-
return executeOkhttp(pool, proxyInfo, uri, data);
37-
} else {
38-
//TODO 这里需要抛出异常,需要优化
39-
return null;
20+
public T execute(RequestHttp requestHttp, String uri, E data) throws WxErrorException, IOException {
21+
switch (requestHttp.getRequestType()) {
22+
case apacheHttp: {
23+
//apache-http请求
24+
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
25+
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
26+
return executeApache(httpClient, httpProxy, uri, data);
27+
}
28+
case joddHttp: {
29+
//jodd-http请求
30+
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
31+
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
32+
return executeJodd(provider, proxyInfo, uri, data);
33+
}
34+
case okHttp: {
35+
//okhttp请求
36+
ConnectionPool pool = (ConnectionPool) requestHttp.getRequestHttpClient();
37+
OkhttpProxyInfo proxyInfo = (OkhttpProxyInfo) requestHttp.getRequestHttpProxy();
38+
return executeOkhttp(pool, proxyInfo, uri, data);
39+
}
40+
default:
41+
//TODO 这里需要抛出异常,需要优化
42+
return null;
4043
}
4144
}
4245

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package me.chanjar.weixin.common.util.http;
2+
3+
/**
4+
* Created by ecoolper on 2017/4/28.
5+
*/
6+
public enum HttpType {
7+
joddHttp, apacheHttp, okHttp;
8+
}

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaDownloadRequestExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public Request authenticate(Route route, Response response) throws IOException {
213213
String contentType = response.header("Content-Type");
214214
if (contentType != null && contentType.startsWith("application/json")) {
215215
// application/json; encoding=utf-8 下载媒体文件出错
216-
throw new WxErrorException(WxError.fromJson(response.body().toString()));
216+
throw new WxErrorException(WxError.fromJson(response.body().string()));
217217
}
218218

219219
String fileName = getFileName(response);

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaUploadRequestExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public Request authenticate(Route route, Response response) throws IOException {
136136
Request request = new Request.Builder().url(uri).post(body).build();
137137

138138
Response response = client.newCall(request).execute();
139-
String responseContent = response.body().toString();
139+
String responseContent = response.body().string();
140140
WxError error = WxError.fromJson(responseContent);
141141
if (error.getErrorCode() != 0) {
142142
throw new WxErrorException(error);

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/RequestHttp.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,10 @@ public interface RequestHttp<H,P> {
1717
*/
1818
P getRequestHttpProxy();
1919

20+
/**
21+
*
22+
* @return
23+
*/
24+
HttpType getRequestType();
25+
2026
}

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimpleGetRequestExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public Request authenticate(Route route, Response response) throws IOException {
141141
Request request = new Request.Builder().url(uri).build();
142142

143143
Response response = client.newCall(request).execute();
144-
String responseContent = response.body().toString();
144+
String responseContent = response.body().string();
145145
WxError error = WxError.fromJson(responseContent);
146146
if (error.getErrorCode() != 0) {
147147
throw new WxErrorException(error);

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimplePostRequestExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public Request authenticate(Route route, Response response) throws IOException {
156156
Request request = new Request.Builder().url(uri).post(body).build();
157157

158158
Response response = client.newCall(request).execute();
159-
String responseContent = response.body().toString();
159+
String responseContent = response.body().string();
160160
WxError error = WxError.fromJson(responseContent);
161161
if (error.getErrorCode() != 0) {
162162
throw new WxErrorException(error);

0 commit comments

Comments
 (0)