use of me.chanjar.weixin.common.bean.result.WxError in project weixin-java-tools by chanjarster.
the class MaterialVideoInfoRequestExecutor method execute.
public WxMpMaterialVideoInfoResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, ClientProtocolException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(config);
}
Map<String, String> params = new HashMap<>();
params.put("media_id", materialId);
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
CloseableHttpResponse response = httpclient.execute(httpPost);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpMaterialVideoInfoResult.fromJson(responseContent);
}
}
use of me.chanjar.weixin.common.bean.result.WxError in project weixin-java-tools by chanjarster.
the class MaterialVoiceAndImageDownloadRequestExecutor method execute.
public InputStream execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, ClientProtocolException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(config);
}
Map<String, String> params = new HashMap<>();
params.put("media_id", materialId);
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
CloseableHttpResponse response = httpclient.execute(httpPost);
// 下载媒体文件出错
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);
byte[] responseContent = IOUtils.toByteArray(inputStream);
String responseContentString = new String(responseContent, "UTF-8");
if (responseContentString.length() < 100) {
try {
WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class);
if (wxError.getErrorCode() != 0) {
throw new WxErrorException(wxError);
}
} catch (com.google.gson.JsonSyntaxException ex) {
return new ByteArrayInputStream(responseContent);
}
}
return new ByteArrayInputStream(responseContent);
}
use of me.chanjar.weixin.common.bean.result.WxError in project weixin-java-tools by chanjarster.
the class WxMpServiceImpl method executeInternal.
protected synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
if (uri.indexOf("access_token=") != -1) {
throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri);
}
String accessToken = getAccessToken(false);
String uriWithAccessToken = uri;
uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken;
try {
return executor.execute(getHttpclient(), httpProxy, uriWithAccessToken, data);
} catch (WxErrorException e) {
WxError error = e.getError();
/*
* 发生以下情况时尝试刷新access_token
* 40001 获取access_token时AppSecret错误,或者access_token无效
* 42001 access_token超时
*/
if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001) {
// 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
wxMpConfigStorage.expireAccessToken();
return execute(executor, uri, data);
}
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
return null;
} catch (ClientProtocolException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Aggregations