Search in sources :

Example 6 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 7 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 8 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)

Example 9 with WxErrorException

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

the class MaterialDeleteRequestExecutor method execute.

public Boolean 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 true;
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) StringEntity(org.apache.http.entity.StringEntity) WxError(me.chanjar.weixin.common.bean.result.WxError) HashMap(java.util.HashMap) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 10 with WxErrorException

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

the class MaterialNewsInfoRequestExecutor method execute.

public WxMpMaterialNews 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 WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) StringEntity(org.apache.http.entity.StringEntity) WxError(me.chanjar.weixin.common.bean.result.WxError) HashMap(java.util.HashMap) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

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