🎨 #1189 优化错误异常输出,移除冗余代码

This commit is contained in:
Binary Wang
2019-09-05 12:31:26 +08:00
parent eecd4bec76
commit 8ab4af031a
81 changed files with 789 additions and 800 deletions

View File

@@ -3,6 +3,7 @@ package me.chanjar.weixin.common.util.http;
import java.io.File;
import java.io.IOException;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.apache.ApacheMediaDownloadRequestExecutor;
import me.chanjar.weixin.common.util.http.jodd.JoddHttpMediaDownloadRequestExecutor;
@@ -25,8 +26,8 @@ public abstract class BaseMediaDownloadRequestExecutor<H, P> implements RequestE
}
@Override
public void execute(String uri, String data, ResponseHandler<File> handler) throws WxErrorException, IOException {
handler.handle(this.execute(uri, data));
public void execute(String uri, String data, ResponseHandler<File> handler, WxType wxType) throws WxErrorException, IOException {
handler.handle(this.execute(uri, data, wxType));
}
public static RequestExecutor<File, String> create(RequestHttp requestHttp, File tmpDirFile) {

View File

@@ -3,6 +3,7 @@ package me.chanjar.weixin.common.util.http;
import java.io.File;
import java.io.IOException;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.apache.ApacheMediaUploadRequestExecutor;
@@ -23,8 +24,8 @@ public abstract class MediaUploadRequestExecutor<H, P> implements RequestExecuto
}
@Override
public void execute(String uri, File data, ResponseHandler<WxMediaUploadResult> handler) throws WxErrorException, IOException {
handler.handle(this.execute(uri, data));
public void execute(String uri, File data, ResponseHandler<WxMediaUploadResult> handler, WxType wxType) throws WxErrorException, IOException {
handler.handle(this.execute(uri, data, wxType));
}
public static RequestExecutor<WxMediaUploadResult, File> create(RequestHttp requestHttp) {

View File

@@ -1,9 +1,10 @@
package me.chanjar.weixin.common.util.http;
import java.io.IOException;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxErrorException;
import java.io.IOException;
/**
* http请求执行器.
*
@@ -16,23 +17,24 @@ public interface RequestExecutor<T, E> {
/**
* 执行http请求.
*
* @param uri uri
* @param data 数据
* @param uri uri
* @param data 数据
* @param wxType 微信模块类型
* @return 响应结果
* @throws WxErrorException 自定义异常
* @throws IOException io异常
*/
T execute(String uri, E data) throws WxErrorException, IOException;
T execute(String uri, E data, WxType wxType) throws WxErrorException, IOException;
/**
* 执行http请求.
*
* @param uri uri
* @param data 数据
* @param uri uri
* @param data 数据
* @param handler http响应处理器
* @param wxType 微信模块类型
* @throws WxErrorException 自定义异常
* @throws IOException io异常
*/
void execute(String uri, E data, ResponseHandler<T> handler) throws WxErrorException, IOException;
void execute(String uri, E data, ResponseHandler<T> handler, WxType wxType) throws WxErrorException, IOException;
}

View File

@@ -2,6 +2,7 @@ package me.chanjar.weixin.common.util.http;
import java.io.IOException;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientSimpleGetRequestExecutor;
import me.chanjar.weixin.common.util.http.jodd.JoddHttpSimpleGetRequestExecutor;
@@ -21,8 +22,8 @@ public abstract class SimpleGetRequestExecutor<H, P> implements RequestExecutor<
}
@Override
public void execute(String uri, String data, ResponseHandler<String> handler) throws WxErrorException, IOException {
handler.handle(this.execute(uri, data));
public void execute(String uri, String data, ResponseHandler<String> handler, WxType wxType) throws WxErrorException, IOException {
handler.handle(this.execute(uri, data, wxType));
}
public static RequestExecutor<String, String> create(RequestHttp requestHttp) {

View File

@@ -2,6 +2,7 @@ package me.chanjar.weixin.common.util.http;
import java.io.IOException;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.apache.ApacheSimplePostRequestExecutor;
import me.chanjar.weixin.common.util.http.jodd.JoddHttpSimplePostRequestExecutor;
@@ -21,8 +22,9 @@ public abstract class SimplePostRequestExecutor<H, P> implements RequestExecutor
}
@Override
public void execute(String uri, String data, ResponseHandler<String> handler) throws WxErrorException, IOException {
handler.handle(this.execute(uri, data));
public void execute(String uri, String data, ResponseHandler<String> handler, WxType wxType)
throws WxErrorException, IOException {
handler.handle(this.execute(uri, data, wxType));
}
public static RequestExecutor<String, String> create(RequestHttp requestHttp) {

View File

@@ -1,29 +1,31 @@
package me.chanjar.weixin.common.util.http.apache;
import java.io.IOException;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
import java.io.IOException;
/**
* Created by ecoolper on 2017/5/4.
* .
*
* @author ecoolper
* @date 2017/5/4
*/
public class ApacheHttpClientSimpleGetRequestExecutor extends SimpleGetRequestExecutor<CloseableHttpClient, HttpHost> {
public ApacheHttpClientSimpleGetRequestExecutor(RequestHttp requestHttp) {
super(requestHttp);
}
@Override
public String execute(String uri, String queryParam) throws WxErrorException, IOException {
public String execute(String uri, String queryParam, WxType wxType) throws WxErrorException, IOException {
if (queryParam != null) {
if (uri.indexOf('?') == -1) {
uri += '?';
@@ -38,7 +40,7 @@ public class ApacheHttpClientSimpleGetRequestExecutor extends SimpleGetRequestEx
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpGet)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
WxError error = WxError.fromJson(responseContent, wxType);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}

View File

@@ -1,9 +1,12 @@
package me.chanjar.weixin.common.util.http.apache;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.BaseMediaDownloadRequestExecutor;
import me.chanjar.weixin.common.util.http.HttpResponseProxy;
import me.chanjar.weixin.common.util.http.RequestHttp;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
@@ -14,15 +17,15 @@ import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.BaseMediaDownloadRequestExecutor;
import me.chanjar.weixin.common.util.http.HttpResponseProxy;
import me.chanjar.weixin.common.util.http.RequestHttp;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by ecoolper on 2017/5/5.
* .
*
* @author ecoolper
* @date 2017/5/5
*/
public class ApacheMediaDownloadRequestExecutor extends BaseMediaDownloadRequestExecutor<CloseableHttpClient, HttpHost> {
public ApacheMediaDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) {
@@ -30,7 +33,7 @@ public class ApacheMediaDownloadRequestExecutor extends BaseMediaDownloadRequest
}
@Override
public File execute(String uri, String queryParam) throws WxErrorException, IOException {
public File execute(String uri, String queryParam, WxType wxType) throws WxErrorException, IOException {
if (queryParam != null) {
if (uri.indexOf('?') == -1) {
uri += '?';
@@ -51,7 +54,7 @@ public class ApacheMediaDownloadRequestExecutor extends BaseMediaDownloadRequest
if (contentTypeHeader[0].getValue().startsWith(ContentType.APPLICATION_JSON.getMimeType())) {
// application/json; encoding=utf-8 下载媒体文件出错
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
throw new WxErrorException(WxError.fromJson(responseContent));
throw new WxErrorException(WxError.fromJson(responseContent, wxType));
}
}

View File

@@ -1,7 +1,8 @@
package me.chanjar.weixin.common.util.http.apache;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
@@ -26,7 +27,7 @@ public class ApacheMediaUploadRequestExecutor extends MediaUploadRequestExecutor
}
@Override
public WxMediaUploadResult execute(String uri, File file) throws WxErrorException, IOException {
public WxMediaUploadResult execute(String uri, File file, WxType wxType) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (requestHttp.getRequestHttpProxy() != null) {
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
@@ -42,7 +43,7 @@ public class ApacheMediaUploadRequestExecutor extends MediaUploadRequestExecutor
}
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
WxError error = WxError.fromJson(responseContent, wxType);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}

View File

@@ -1,5 +1,6 @@
package me.chanjar.weixin.common.util.http.apache;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestHttp;
@@ -15,16 +16,18 @@ import org.apache.http.impl.client.CloseableHttpClient;
import java.io.IOException;
/**
* Created by ecoolper on 2017/5/4.
* .
*
* @author ecoolper
* @date 2017/5/4
*/
public class ApacheSimplePostRequestExecutor extends SimplePostRequestExecutor<CloseableHttpClient, HttpHost> {
public ApacheSimplePostRequestExecutor(RequestHttp requestHttp) {
super(requestHttp);
}
@Override
public String execute(String uri, String postEntity) throws WxErrorException, IOException {
public String execute(String uri, String postEntity, WxType wxType) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (requestHttp.getRequestHttpProxy() != null) {
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
@@ -47,7 +50,7 @@ public class ApacheSimplePostRequestExecutor extends SimplePostRequestExecutor<C
return responseContent;
}
WxError error = WxError.fromJson(responseContent);
WxError error = WxError.fromJson(responseContent, wxType);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}

View File

@@ -1,36 +1,38 @@
package me.chanjar.weixin.common.util.http.jodd;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import jodd.http.HttpConnectionProvider;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.http.ProxyInfo;
import jodd.util.StringPool;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.BaseMediaDownloadRequestExecutor;
import me.chanjar.weixin.common.util.http.HttpResponseProxy;
import me.chanjar.weixin.common.util.http.RequestHttp;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by ecoolper on 2017/5/5.
* .
*
* @author ecoolper
* @date 2017/5/5
*/
public class JoddHttpMediaDownloadRequestExecutor extends BaseMediaDownloadRequestExecutor<HttpConnectionProvider, ProxyInfo> {
public JoddHttpMediaDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) {
super(requestHttp, tmpDirFile);
}
@Override
public File execute(String uri, String queryParam) throws WxErrorException, IOException {
public File execute(String uri, String queryParam, WxType wxType) throws WxErrorException, IOException {
if (queryParam != null) {
if (uri.indexOf('?') == -1) {
uri += '?';
@@ -50,7 +52,7 @@ public class JoddHttpMediaDownloadRequestExecutor extends BaseMediaDownloadReque
String contentType = response.header("Content-Type");
if (contentType != null && contentType.startsWith("application/json")) {
// application/json; encoding=utf-8 下载媒体文件出错
throw new WxErrorException(WxError.fromJson(response.bodyText()));
throw new WxErrorException(WxError.fromJson(response.bodyText(), wxType));
}
String fileName = new HttpResponseProxy(response).getFileName();

View File

@@ -5,9 +5,9 @@ import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.http.ProxyInfo;
import jodd.util.StringPool;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
@@ -16,7 +16,10 @@ import java.io.File;
import java.io.IOException;
/**
* Created by ecoolper on 2017/5/5.
* .
*
* @author ecoolper
* @date 2017/5/5
*/
public class JoddHttpMediaUploadRequestExecutor extends MediaUploadRequestExecutor<HttpConnectionProvider, ProxyInfo> {
public JoddHttpMediaUploadRequestExecutor(RequestHttp requestHttp) {
@@ -24,7 +27,7 @@ public class JoddHttpMediaUploadRequestExecutor extends MediaUploadRequestExecut
}
@Override
public WxMediaUploadResult execute(String uri, File file) throws WxErrorException, IOException {
public WxMediaUploadResult execute(String uri, File file, WxType wxType) throws WxErrorException, IOException {
HttpRequest request = HttpRequest.post(uri);
if (requestHttp.getRequestHttpProxy() != null) {
requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
@@ -35,7 +38,7 @@ public class JoddHttpMediaUploadRequestExecutor extends MediaUploadRequestExecut
response.charset(StringPool.UTF_8);
String responseContent = response.bodyText();
WxError error = WxError.fromJson(responseContent);
WxError error = WxError.fromJson(responseContent, wxType);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}

View File

@@ -1,8 +1,11 @@
package me.chanjar.weixin.common.util.http.jodd;
import jodd.http.*;
import jodd.http.HttpConnectionProvider;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.http.ProxyInfo;
import jodd.util.StringPool;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestHttp;
@@ -11,16 +14,18 @@ import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
import java.io.IOException;
/**
* Created by ecoolper on 2017/5/4.
* .
*
* @author ecoolper
* @date 2017/5/4
*/
public class JoddHttpSimpleGetRequestExecutor extends SimpleGetRequestExecutor<HttpConnectionProvider, ProxyInfo> {
public JoddHttpSimpleGetRequestExecutor(RequestHttp requestHttp) {
super(requestHttp);
}
@Override
public String execute(String uri, String queryParam) throws WxErrorException, IOException {
public String execute(String uri, String queryParam, WxType wxType) throws WxErrorException, IOException {
if (queryParam != null) {
if (uri.indexOf('?') == -1) {
uri += '?';
@@ -38,7 +43,7 @@ public class JoddHttpSimpleGetRequestExecutor extends SimpleGetRequestExecutor<H
String responseContent = response.bodyText();
WxError error = WxError.fromJson(responseContent);
WxError error = WxError.fromJson(responseContent, wxType);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}

View File

@@ -5,6 +5,7 @@ import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.http.ProxyInfo;
import jodd.util.StringPool;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestHttp;
@@ -13,16 +14,18 @@ import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
import java.io.IOException;
/**
* Created by ecoolper on 2017/5/4.
* .
*
* @author ecoolper
* @date 2017/5/4
*/
public class JoddHttpSimplePostRequestExecutor extends SimplePostRequestExecutor<HttpConnectionProvider, ProxyInfo> {
public JoddHttpSimplePostRequestExecutor(RequestHttp requestHttp) {
super(requestHttp);
}
@Override
public String execute(String uri, String postEntity) throws WxErrorException, IOException {
public String execute(String uri, String postEntity, WxType wxType) throws WxErrorException, IOException {
HttpConnectionProvider provider = requestHttp.getRequestHttpClient();
ProxyInfo proxyInfo = requestHttp.getRequestHttpProxy();
@@ -39,8 +42,7 @@ public class JoddHttpSimplePostRequestExecutor extends SimplePostRequestExecutor
String responseContent = response.bodyText();
if (responseContent.isEmpty()) {
throw new WxErrorException(WxError.builder().errorCode(9999).errorMsg("无响应内容")
.build());
throw new WxErrorException(WxError.builder().errorCode(9999).errorMsg("无响应内容").build());
}
if (responseContent.startsWith("<xml>")) {
@@ -48,7 +50,7 @@ public class JoddHttpSimplePostRequestExecutor extends SimplePostRequestExecutor
return responseContent;
}
WxError error = WxError.fromJson(responseContent);
WxError error = WxError.fromJson(responseContent, wxType);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}

View File

@@ -1,5 +1,7 @@
package me.chanjar.weixin.common.util.http.okhttp;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.BaseMediaDownloadRequestExecutor;
@@ -19,18 +21,18 @@ import java.io.File;
import java.io.IOException;
/**
* Created by ecoolper on 2017/5/5.
*.
* @author ecoolper
* @date 2017/5/5
*/
@Slf4j
public class OkHttpMediaDownloadRequestExecutor extends BaseMediaDownloadRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public OkHttpMediaDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) {
super(requestHttp, tmpDirFile);
}
@Override
public File execute(String uri, String queryParam) throws WxErrorException, IOException {
logger.debug("OkHttpMediaDownloadRequestExecutor is running");
public File execute(String uri, String queryParam, WxType wxType) throws WxErrorException, IOException {
if (queryParam != null) {
if (uri.indexOf('?') == -1) {
uri += '?';
@@ -48,7 +50,7 @@ public class OkHttpMediaDownloadRequestExecutor extends BaseMediaDownloadRequest
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().string()));
throw new WxErrorException(WxError.fromJson(response.body().string(), wxType));
}
String fileName = new HttpResponseProxy(response).getFileName();

View File

@@ -1,32 +1,29 @@
package me.chanjar.weixin.common.util.http.okhttp;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
/**
* Created by ecoolper on 2017/5/5.
* .
*
* @author ecoolper
* @date 2017/5/5
*/
public class OkHttpMediaUploadRequestExecutor extends MediaUploadRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public OkHttpMediaUploadRequestExecutor(RequestHttp requestHttp) {
super(requestHttp);
}
@Override
public WxMediaUploadResult execute(String uri, File file) throws WxErrorException, IOException {
logger.debug("OkHttpMediaUploadRequestExecutor is running");
//得到httpClient
OkHttpClient client = requestHttp.getRequestHttpClient();
public WxMediaUploadResult execute(String uri, File file, WxType wxType) throws WxErrorException, IOException {
RequestBody body = new MultipartBody.Builder()
.setType(MediaType.parse("multipart/form-data"))
@@ -36,9 +33,9 @@ public class OkHttpMediaUploadRequestExecutor extends MediaUploadRequestExecutor
.build();
Request request = new Request.Builder().url(uri).post(body).build();
Response response = client.newCall(request).execute();
Response response = requestHttp.getRequestHttpClient().newCall(request).execute();
String responseContent = response.body().string();
WxError error = WxError.fromJson(responseContent);
WxError error = WxError.fromJson(responseContent, wxType);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}

View File

@@ -1,28 +1,29 @@
package me.chanjar.weixin.common.util.http.okhttp;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
/**
* Created by ecoolper on 2017/5/4.
* .
*
* @author ecoolper
* @date 2017/5/4
*/
public class OkHttpSimpleGetRequestExecutor extends SimpleGetRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public OkHttpSimpleGetRequestExecutor(RequestHttp requestHttp) {
super(requestHttp);
}
@Override
public String execute(String uri, String queryParam) throws WxErrorException, IOException {
logger.debug("OkHttpSimpleGetRequestExecutor is running");
public String execute(String uri, String queryParam, WxType wxType) throws WxErrorException, IOException {
if (queryParam != null) {
if (uri.indexOf('?') == -1) {
uri += '?';
@@ -35,7 +36,7 @@ public class OkHttpSimpleGetRequestExecutor extends SimpleGetRequestExecutor<OkH
Request request = new Request.Builder().url(uri).build();
Response response = client.newCall(request).execute();
String responseContent = response.body().string();
WxError error = WxError.fromJson(responseContent);
WxError error = WxError.fromJson(responseContent, wxType);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}

View File

@@ -1,42 +1,38 @@
package me.chanjar.weixin.common.util.http.okhttp;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
/**
* Created by ecoolper on 2017/5/4.
* .
*
* @author ecoolper
* @date 2017/5/4
*/
@Slf4j
public class OkHttpSimplePostRequestExecutor extends SimplePostRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public OkHttpSimplePostRequestExecutor(RequestHttp requestHttp) {
super(requestHttp);
}
@Override
public String execute(String uri, String postEntity) throws WxErrorException, IOException {
logger.debug("OkHttpSimplePostRequestExecutor running");
//得到httpClient
OkHttpClient client = requestHttp.getRequestHttpClient();
MediaType mediaType = MediaType.parse("text/plain; charset=utf-8");
RequestBody body = RequestBody.create(mediaType, postEntity);
public String execute(String uri, String postEntity, WxType wxType) throws WxErrorException, IOException {
RequestBody body = RequestBody.create(MediaType.parse("text/plain; charset=utf-8"), postEntity);
Request request = new Request.Builder().url(uri).post(body).build();
Response response = client.newCall(request).execute();
Response response = requestHttp.getRequestHttpClient().newCall(request).execute();
String responseContent = response.body().string();
WxError error = WxError.fromJson(responseContent);
WxError error = WxError.fromJson(responseContent, wxType);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
return responseContent;
}