Search in sources :

Example 6 with WxError

use of me.chanjar.weixin.common.bean.result.WxError 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 7 with WxError

use of me.chanjar.weixin.common.bean.result.WxError 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 8 with WxError

use of me.chanjar.weixin.common.bean.result.WxError 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)

Example 9 with WxError

use of me.chanjar.weixin.common.bean.result.WxError in project weixin-java-tools by chanjarster.

the class WxMpServiceImpl method sendRedpack.

@Override
public WxRedpackResult sendRedpack(Map<String, String> parameters) throws WxErrorException {
    String nonce_str = System.currentTimeMillis() + "";
    SortedMap<String, String> packageParams = new TreeMap<String, String>(parameters);
    packageParams.put("wxappid", wxMpConfigStorage.getAppId());
    packageParams.put("mch_id", wxMpConfigStorage.getPartnerId());
    packageParams.put("nonce_str", nonce_str);
    String sign = WxCryptUtil.createSign(packageParams, wxMpConfigStorage.getPartnerKey());
    packageParams.put("sign", sign);
    StringBuilder request = new StringBuilder("<xml>");
    for (Entry<String, String> para : packageParams.entrySet()) {
        request.append(String.format("<%s>%s</%s>", para.getKey(), para.getValue(), para.getKey()));
    }
    request.append("</xml>");
    HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack");
    if (httpProxy != null) {
        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
        httpPost.setConfig(config);
    }
    StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
    httpPost.setEntity(entity);
    try {
        CloseableHttpResponse response = getHttpclient().execute(httpPost);
        String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
        XStream xstream = XStreamInitializer.getInstance();
        xstream.processAnnotations(WxRedpackResult.class);
        WxRedpackResult wxMpRedpackResult = (WxRedpackResult) xstream.fromXML(responseContent);
        return wxMpRedpackResult;
    } catch (IOException e) {
        log.error(MessageFormatter.format("The exception was happened when sending redpack '{}'.", request.toString()).getMessage(), e);
        WxError error = new WxError();
        error.setErrorCode(-1);
        throw new WxErrorException(error);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) WxError(me.chanjar.weixin.common.bean.result.WxError) XStream(com.thoughtworks.xstream.XStream) IOException(java.io.IOException) TreeMap(java.util.TreeMap) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) WxRedpackResult(me.chanjar.weixin.mp.bean.result.WxRedpackResult)

Example 10 with WxError

use of me.chanjar.weixin.common.bean.result.WxError in project weixin-java-tools by chanjarster.

the class WxErrorTest method testFromBadJson2.

public void testFromBadJson2() {
    String json = "{\"access_token\":\"ACCESS_TOKEN\",\"expires_in\":7200}";
    WxError wxError = WxError.fromJson(json);
    Assert.assertTrue(wxError.getErrorCode() == 0);
    Assert.assertEquals(wxError.getErrorMsg(), null);
}
Also used : WxError(me.chanjar.weixin.common.bean.result.WxError)

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