use of me.chanjar.weixin.common.bean.result.WxError in project weixin-java-tools by chanjarster.
the class WxErrorTest method testFromJson.
public void testFromJson() {
String json = "{ \"errcode\": 40003, \"errmsg\": \"invalid openid\" }";
WxError wxError = WxError.fromJson(json);
Assert.assertTrue(wxError.getErrorCode() == 40003);
Assert.assertEquals(wxError.getErrorMsg(), "invalid openid");
}
use of me.chanjar.weixin.common.bean.result.WxError in project weixin-java-tools by chanjarster.
the class SimpleGetRequestExecutor method execute.
@Override
public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, ClientProtocolException, IOException {
if (queryParam != null) {
if (uri.indexOf('?') == -1) {
uri += '?';
}
uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
}
HttpGet httpGet = new HttpGet(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpGet.setConfig(config);
}
try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
return responseContent;
}
}
use of me.chanjar.weixin.common.bean.result.WxError in project weixin-java-tools by chanjarster.
the class WxErrorAdapter method deserialize.
public WxError deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
WxError wxError = new WxError();
JsonObject wxErrorJsonObject = json.getAsJsonObject();
if (wxErrorJsonObject.get("errcode") != null && !wxErrorJsonObject.get("errcode").isJsonNull()) {
wxError.setErrorCode(GsonHelper.getAsPrimitiveInt(wxErrorJsonObject.get("errcode")));
}
if (wxErrorJsonObject.get("errmsg") != null && !wxErrorJsonObject.get("errmsg").isJsonNull()) {
wxError.setErrorMsg(GsonHelper.getAsString(wxErrorJsonObject.get("errmsg")));
}
wxError.setJson(json.toString());
return wxError;
}
use of me.chanjar.weixin.common.bean.result.WxError in project weixin-java-tools by chanjarster.
the class WxMpBusyRetryTest method getService.
@DataProvider(name = "getService")
public Object[][] getService() {
WxMpService service = new WxMpServiceImpl() {
@Override
protected <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
WxError error = new WxError();
error.setErrorCode(-1);
throw new WxErrorException(error);
}
};
service.setMaxRetryTimes(3);
service.setRetrySleepMillis(500);
return new Object[][] { new Object[] { service } };
}
use of me.chanjar.weixin.common.bean.result.WxError in project weixin-java-tools by chanjarster.
the class WxMpServiceImpl method materialNewsBatchGet.
public WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material";
Map<String, Object> params = new HashMap<>();
params.put("type", WxConsts.MATERIAL_NEWS);
params.put("offset", offset);
params.put("count", count);
String responseText = post(url, WxGsonBuilder.create().toJson(params));
WxError wxError = WxError.fromJson(responseText);
if (wxError.getErrorCode() == 0) {
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialNewsBatchGetResult.class);
} else {
throw new WxErrorException(wxError);
}
}
Aggregations