Search in sources :

Example 1 with WxError

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");
}
Also used : WxError(me.chanjar.weixin.common.bean.result.WxError)

Example 2 with WxError

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;
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) WxError(me.chanjar.weixin.common.bean.result.WxError) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 3 with WxError

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;
}
Also used : WxError(me.chanjar.weixin.common.bean.result.WxError)

Example 4 with 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 } };
}
Also used : WxError(me.chanjar.weixin.common.bean.result.WxError) RequestExecutor(me.chanjar.weixin.common.util.http.RequestExecutor) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException) DataProvider(org.testng.annotations.DataProvider)

Example 5 with WxError

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);
    }
}
Also used : WxError(me.chanjar.weixin.common.bean.result.WxError) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Aggregations

WxError (me.chanjar.weixin.common.bean.result.WxError)23 WxErrorException (me.chanjar.weixin.common.exception.WxErrorException)19 RequestConfig (org.apache.http.client.config.RequestConfig)11 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)11 HttpPost (org.apache.http.client.methods.HttpPost)8 HashMap (java.util.HashMap)6 StringEntity (org.apache.http.entity.StringEntity)6 IOException (java.io.IOException)5 ClientProtocolException (org.apache.http.client.ClientProtocolException)4 HttpGet (org.apache.http.client.methods.HttpGet)3 JsonObject (com.google.gson.JsonObject)2 WxAccessToken (me.chanjar.weixin.common.bean.WxAccessToken)2 RequestExecutor (me.chanjar.weixin.common.util.http.RequestExecutor)2 BasicResponseHandler (org.apache.http.impl.client.BasicResponseHandler)2 DataProvider (org.testng.annotations.DataProvider)2 XStream (com.thoughtworks.xstream.XStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 TreeMap (java.util.TreeMap)1 WxRedpackResult (me.chanjar.weixin.mp.bean.result.WxRedpackResult)1