me.chanjar.weixin.mp.util.json.WxMpGsonBuilder Java Examples

The following examples show how to use me.chanjar.weixin.mp.util.json.WxMpGsonBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WxMpCardServiceImpl.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
/**
 * 卡券Mark接口。
 * 开发者在帮助消费者核销卡券之前,必须帮助先将此code(卡券串码)与一个openid绑定(即mark住),
 * 才能进一步调用核销接口,否则报错。
 *
 * @param code   卡券的code码
 * @param cardId 卡券的ID
 * @param openId 用券用户的openid
 * @param isMark 是否要mark(占用)这个code,填写true或者false,表示占用或解除占用
 */
@Override
public void markCardCode(String code, String cardId, String openId, boolean isMark) throws
  WxErrorException {
  JsonObject param = new JsonObject();
  param.addProperty("code", code);
  param.addProperty("card_id", cardId);
  param.addProperty("openid", openId);
  param.addProperty("is_mark", isMark);
  String responseContent = this.getWxMpService().post(CARD_CODE_MARK, param.toString());
  JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
  WxMpCardResult cardResult = WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement,
    new TypeToken<WxMpCardResult>() {
    }.getType());
  if (!"0".equals(cardResult.getErrorCode())) {
    this.log.warn("朋友的券mark失败:{}", cardResult.getErrorMsg());
  }
}
 
Example #2
Source File: MaterialNewsInfoRequestExecutor.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
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);
  }
}
 
Example #3
Source File: MaterialNewsInfoOkhttpRequestExecutor.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
@Override
public WxMpMaterialNews execute(String uri, String materialId) throws WxErrorException, IOException {

  //得到httpClient
  OkHttpClient client = requestHttp.getRequestHttpClient();

  RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),
    WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId)));
  Request request = new Request.Builder().url(uri).post(requestBody).build();

  Response response = client.newCall(request).execute();
  String responseContent = response.body().string();
  this.logger.debug("响应原始数据:{}", responseContent);

  WxError error = WxError.fromJson(responseContent, WxType.MP);
  if (error.getErrorCode() != 0) {
    throw new WxErrorException(error);
  } else {
    return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class);
  }
}
 
Example #4
Source File: MaterialNewsInfoApacheHttpRequestExecutor.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
@Override
public WxMpMaterialNews execute(String uri, String materialId) throws WxErrorException, IOException {
  HttpPost httpPost = new HttpPost(uri);
  if (requestHttp.getRequestHttpProxy() != null) {
    RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
    httpPost.setConfig(config);
  }

  httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId))));
  try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
    String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
    this.logger.debug("响应原始数据:{}", responseContent);
    WxError error = WxError.fromJson(responseContent, WxType.MP);
    if (error.getErrorCode() != 0) {
      throw new WxErrorException(error);
    } else {
      return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class);
    }
  } finally {
    httpPost.releaseConnection();
  }
}
 
Example #5
Source File: MaterialNewsInfoJoddHttpRequestExecutor.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
@Override
public WxMpMaterialNews execute(String uri, String materialId) throws WxErrorException, IOException {
  if (requestHttp.getRequestHttpProxy() != null) {
    requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }

  HttpRequest request = HttpRequest.post(uri)
    .withConnectionProvider(requestHttp.getRequestHttpClient())
    .body(WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId)));
  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);

  String responseContent = response.bodyText();
  this.logger.debug("响应原始数据:{}", responseContent);
  WxError error = WxError.fromJson(responseContent, WxType.MP);
  if (error.getErrorCode() != 0) {
    throw new WxErrorException(error);
  } else {
    return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class);
  }
}
 
Example #6
Source File: WxMpServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public List<WxMpUserCumulate> getUserCumulate(Date beginDate, Date endDate) throws WxErrorException {
  String url = "https://api.weixin.qq.com/datacube/getusercumulate";
  JsonObject param = new JsonObject();
  param.addProperty("begin_date", SIMPLE_DATE_FORMAT.format(beginDate));
  param.addProperty("end_date", SIMPLE_DATE_FORMAT.format(endDate));
  String responseContent = post(url, param.toString());
  JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
  return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("list"),
      new TypeToken<List<WxMpUserCumulate>>() {
      }.getType());
}
 
Example #7
Source File: WxMpMaterialServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException {
  Map<String, Object> params = new HashMap<>();
  params.put("type", type);
  params.put("offset", offset);
  params.put("count", count);
  String responseText = this.wxMpService.post(MATERIAL_BATCHGET_URL, WxGsonBuilder.create().toJson(params));
  WxError wxError = WxError.fromJson(responseText, WxType.MP);
  if (wxError.getErrorCode() == 0) {
    return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialFileBatchGetResult.class);
  } else {
    throw new WxErrorException(wxError);
  }
}
 
Example #8
Source File: WxMpMaterialServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public WxMpMaterialCountResult materialCount() throws WxErrorException {
  String responseText = this.wxMpService.get(MATERIAL_GET_COUNT_URL, null);
  WxError wxError = WxError.fromJson(responseText, WxType.MP);
  if (wxError.getErrorCode() == 0) {
    return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialCountResult.class);
  } else {
    throw new WxErrorException(wxError);
  }
}
 
Example #9
Source File: WxMpMemberCardServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
/**
 * 当会员持卡消费后,支持开发者调用该接口更新会员信息。会员卡交易后的每次信息变更需通过该接口通知微信,便于后续消息通知及其他扩展功能。
 *
 * 1.开发者可以同时传入add_bonus和bonus解决由于同步失败带来的幂等性问题。同时传入add_bonus和bonus时
 * add_bonus作为积分变动消息中的变量值,而bonus作为卡面上的总积分额度显示。余额变动同理。
 * 2.开发者可以传入is_notify_bonus控制特殊的积分对账变动不发送消息,余额变动同理。
 *
 * @param updateUserMessage 更新会员信息所需字段消息
 * @return 调用返回的JSON字符串。
 * @throws WxErrorException 接口调用失败抛出的异常
 */
@Override
public WxMpMemberCardUpdateResult updateUserMemberCard(WxMpMemberCardUpdateMessage updateUserMessage)
  throws WxErrorException {

  String responseContent = this.getWxMpService().post(MEMBER_CARD_UPDATE_USER, GSON.toJson(updateUserMessage));

  JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
  return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement,
    new TypeToken<WxMpMemberCardUpdateResult>() {
    }.getType());
}
 
Example #10
Source File: WxMpServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
public List<WxMpGroup> groupGet() throws WxErrorException {
  String url = "https://api.weixin.qq.com/cgi-bin/groups/get";
  String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
  /*
   * 操蛋的微信API,创建时返回的是 { group : { id : ..., name : ...} }
   * 查询时返回的是 { groups : [ { id : ..., name : ..., count : ... }, ... ] }
   */
  JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
  return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("groups"),
      new TypeToken<List<WxMpGroup>>() {
      }.getType());
}
 
Example #11
Source File: WxMpMaterialServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count) throws WxErrorException {
  Map<String, Object> params = new HashMap<>();
  params.put("type", WxConsts.MaterialType.NEWS);
  params.put("offset", offset);
  params.put("count", count);
  String responseText = this.wxMpService.post(MATERIAL_BATCHGET_URL, WxGsonBuilder.create().toJson(params));
  WxError wxError = WxError.fromJson(responseText, WxType.MP);
  if (wxError.getErrorCode() == 0) {
    return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialNewsBatchGetResult.class);
  } else {
    throw new WxErrorException(wxError);
  }
}
 
Example #12
Source File: WxMpServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example #13
Source File: WxMpStoreBaseInfo.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
public String toJson() {
  JsonElement baseInfo = WxMpGsonBuilder.create().toJsonTree(this);
  JsonObject jsonObject = new JsonObject();
  jsonObject.add("base_info", baseInfo);
  JsonObject business = new JsonObject();
  business.add("business", jsonObject);
  return business.toString();
}
 
Example #14
Source File: WxMpUser.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
public static List<WxMpUser> fromJsonList(String json) {
  Type collectionType = new TypeToken<List<WxMpUser>>() {
  }.getType();
  Gson gson = WxMpGsonBuilder.INSTANCE.create();
  JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
  return gson.fromJson(jsonObject.get("user_info_list"), collectionType);
}
 
Example #15
Source File: WxMpShakeAroundPageAddResult.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
public static WxMpShakeAroundPageAddResult fromJson(String json) {
  JsonObject jsonObject = WxMpGsonBuilder.INSTANCE.create().fromJson(json, JsonObject.class);
  WxMpShakeAroundPageAddResult result = new WxMpShakeAroundPageAddResult();
  result.setErrorCode(GsonHelper.getInteger(jsonObject, "errcode"));
  result.setErrorMsg(GsonHelper.getString(jsonObject, "errmsg"));
  jsonObject = jsonObject.getAsJsonObject("data");
  if(jsonObject != null){
    result.setPageId(GsonHelper.getInteger(jsonObject, "page_id"));
  }
  return result;
}
 
Example #16
Source File: WxMpServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example #17
Source File: WxMpServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public List<WxMpUserSummary> getUserSummary(Date beginDate, Date endDate) throws WxErrorException {
  String url = "https://api.weixin.qq.com/datacube/getusersummary";
  JsonObject param = new JsonObject();
  param.addProperty("begin_date", SIMPLE_DATE_FORMAT.format(beginDate));
  param.addProperty("end_date", SIMPLE_DATE_FORMAT.format(endDate));
  String responseContent = post(url, param.toString());
  JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
  return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("list"),
      new TypeToken<List<WxMpUserSummary>>() {
      }.getType());
}
 
Example #18
Source File: WxMpMaterialNews.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
public String toJson() {
  return WxMpGsonBuilder.INSTANCE.create().toJson(this);
}
 
Example #19
Source File: WxMpMaterialUploadResult.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
public static WxMpMaterialUploadResult fromJson(String json) {
  return WxMpGsonBuilder.create().fromJson(json, WxMpMaterialUploadResult.class);
}
 
Example #20
Source File: WxMpUserBlacklistGetResult.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
public static WxMpUserBlacklistGetResult fromJson(String json) {
  return WxMpGsonBuilder.INSTANCE.create().fromJson(json, WxMpUserBlacklistGetResult.class);
}
 
Example #21
Source File: WxMpKfMsgList.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
public static WxMpKfMsgList fromJson(String responseContent) {
  return WxMpGsonBuilder.INSTANCE.create().fromJson(responseContent, WxMpKfMsgList.class);
}
 
Example #22
Source File: WxMpOAuth2AccessToken.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
public static WxMpOAuth2AccessToken fromJson(String json) {
  return WxMpGsonBuilder.create().fromJson(json, WxMpOAuth2AccessToken.class);
}
 
Example #23
Source File: WxMpSubscribeMessage.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
public String toJson() {
  return WxMpGsonBuilder.INSTANCE.create().toJson(this);
}
 
Example #24
Source File: WxMpMaterialArticleUpdate.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
public String toJson() {
  return WxMpGsonBuilder.create().toJson(this);
}
 
Example #25
Source File: WxMpTemplateIndustry.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
public String toJson() {
  return WxMpGsonBuilder.create().toJson(this);
}
 
Example #26
Source File: WxMpTemplateMessage.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
public String toJson() {
  return WxMpGsonBuilder.INSTANCE.create().toJson(this);
}
 
Example #27
Source File: WxMpTemplate.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
public static List<WxMpTemplate> fromJson(String json) {
  return WxMpGsonBuilder.create().fromJson(JSON_PARSER.parse(json).getAsJsonObject().get("template_list"),
    new TypeToken<List<WxMpTemplate>>() {
    }.getType());
}
 
Example #28
Source File: WxMpShakeAroundRelationSearchResult.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
public static WxMpShakeAroundRelationSearchResult fromJson(String json) {
  return WxMpGsonBuilder.INSTANCE.create().fromJson(json, WxMpShakeAroundRelationSearchResult.class);
}
 
Example #29
Source File: WxMpOAuth2AccessToken.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
public static WxMpOAuth2AccessToken fromJson(String json) {
  return WxMpGsonBuilder.create().fromJson(json, WxMpOAuth2AccessToken.class);
}
 
Example #30
Source File: WxMpMaterialVideoInfoResult.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
public static WxMpMaterialVideoInfoResult fromJson(String json) {
  return WxMpGsonBuilder.create().fromJson(json, WxMpMaterialVideoInfoResult.class);
}