Search in sources :

Example 1 with WxErrorException

use of me.chanjar.weixin.common.exception.WxErrorException 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 2 with WxErrorException

use of me.chanjar.weixin.common.exception.WxErrorException in project weixin-java-tools by chanjarster.

the class WxMpServiceImpl method templateSend.

public String templateSend(WxMpTemplateMessage templateMessage) throws WxErrorException {
    String url = "https://api.weixin.qq.com/cgi-bin/message/template/send";
    String responseContent = execute(new SimplePostRequestExecutor(), url, templateMessage.toJson());
    JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
    final JsonObject jsonObject = tmpJsonElement.getAsJsonObject();
    if (jsonObject.get("errcode").getAsInt() == 0)
        return jsonObject.get("msgid").getAsString();
    throw new WxErrorException(WxError.fromJson(responseContent));
}
Also used : JsonElement(com.google.gson.JsonElement) StringReader(java.io.StringReader) JsonReader(com.google.gson.stream.JsonReader) JsonObject(com.google.gson.JsonObject) SimplePostRequestExecutor(me.chanjar.weixin.common.util.http.SimplePostRequestExecutor) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 3 with WxErrorException

use of me.chanjar.weixin.common.exception.WxErrorException 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)

Example 4 with WxErrorException

use of me.chanjar.weixin.common.exception.WxErrorException in project weixin-java-tools by chanjarster.

the class WxMpServiceImpl method materialCount.

public WxMpMaterialCountResult materialCount() throws WxErrorException {
    String url = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount";
    String responseText = get(url, null);
    WxError wxError = WxError.fromJson(responseText);
    if (wxError.getErrorCode() == 0) {
        return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialCountResult.class);
    } else {
        throw new WxErrorException(wxError);
    }
}
Also used : WxError(me.chanjar.weixin.common.bean.result.WxError) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 5 with WxErrorException

use of me.chanjar.weixin.common.exception.WxErrorException in project weixin-java-tools by chanjarster.

the class WxCpBusyRetryTest method testRetryInThreadPool.

@Test(dataProvider = "getService")
public void testRetryInThreadPool(final WxCpService service) throws InterruptedException, ExecutionException {
    // 当线程池中的线程复用的时候,还是能保证相同的重试次数
    ExecutorService executorService = Executors.newFixedThreadPool(1);
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            try {
                System.out.println("=====================");
                System.out.println(Thread.currentThread().getName() + ": testRetry");
                service.execute(null, null, null);
            } catch (WxErrorException e) {
                throw new RuntimeException(e);
            } catch (RuntimeException e) {
            // OK
            }
        }
    };
    Future<?> submit1 = executorService.submit(runnable);
    Future<?> submit2 = executorService.submit(runnable);
    submit1.get();
    submit2.get();
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException) Test(org.testng.annotations.Test)

Aggregations

WxErrorException (me.chanjar.weixin.common.exception.WxErrorException)28 WxError (me.chanjar.weixin.common.bean.result.WxError)19 RequestConfig (org.apache.http.client.config.RequestConfig)13 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)13 HashMap (java.util.HashMap)8 HttpPost (org.apache.http.client.methods.HttpPost)8 IOException (java.io.IOException)6 StringEntity (org.apache.http.entity.StringEntity)6 HttpGet (org.apache.http.client.methods.HttpGet)5 ClientProtocolException (org.apache.http.client.ClientProtocolException)4 JsonObject (com.google.gson.JsonObject)3 InputStream (java.io.InputStream)3 File (java.io.File)2 ExecutorService (java.util.concurrent.ExecutorService)2 WxAccessToken (me.chanjar.weixin.common.bean.WxAccessToken)2 RequestExecutor (me.chanjar.weixin.common.util.http.RequestExecutor)2 Header (org.apache.http.Header)2 BasicResponseHandler (org.apache.http.impl.client.BasicResponseHandler)2 DataProvider (org.testng.annotations.DataProvider)2 Test (org.testng.annotations.Test)2