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

The following examples show how to use me.chanjar.weixin.mp.util.json.WxMpGsonBuilder. These examples are extracted from open source projects. 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 Project: weixin-java-tools   Source File: WxMpCardServiceImpl.java    License: 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
@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 3
@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 4
@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 5
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 6
/**
 * 当会员持卡消费后,支持开发者调用该接口更新会员信息。会员卡交易后的每次信息变更需通过该接口通知微信,便于后续消息通知及其他扩展功能。
 *
 * 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 7
Source Project: weixin-java-tools   Source File: WxMpMaterialServiceImpl.java    License: 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 8
Source Project: weixin-java-tools   Source File: WxMpMaterialServiceImpl.java    License: 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 9
Source Project: weixin-java-tools   Source File: WxMpMaterialServiceImpl.java    License: 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 10
Source Project: weixin-java-tools   Source File: WxMpServiceImpl.java    License: 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 11
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 12
Source Project: weixin-java-tools   Source File: WxMpUser.java    License: 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 13
Source Project: weixin-java-tools   Source File: WxMpStoreBaseInfo.java    License: 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 Project: weixin-java-tools   Source File: WxMpServiceImpl.java    License: 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 15
Source Project: weixin-java-tools   Source File: WxMpServiceImpl.java    License: 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 16
Source Project: weixin-java-tools   Source File: WxMpServiceImpl.java    License: 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 17
Source Project: weixin-java-tools   Source File: WxMpServiceImpl.java    License: 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 Project: weixin-java-tools   Source File: WxMpUserList.java    License: Apache License 2.0 4 votes vote down vote up
public static WxMpUserList fromJson(String json) {
  return WxMpGsonBuilder.INSTANCE.create().fromJson(json, WxMpUserList.class);
}
 
Example 19
Source Project: weixin-java-tools   Source File: WxMpSemanticQueryResult.java    License: Apache License 2.0 4 votes vote down vote up
public static WxMpSemanticQueryResult fromJson(String json) {
  return WxMpGsonBuilder.create().fromJson(json, WxMpSemanticQueryResult.class);
}
 
Example 20
Source Project: weixin-java-tools   Source File: WxMpMassNews.java    License: Apache License 2.0 4 votes vote down vote up
public String toJson() {
  return WxMpGsonBuilder.INSTANCE.create().toJson(this);
}
 
Example 21
Source Project: weixin-java-tools   Source File: WxMpMassGroupMessage.java    License: Apache License 2.0 4 votes vote down vote up
public String toJson() {
  return WxMpGsonBuilder.INSTANCE.create().toJson(this);
}
 
Example 22
Source Project: weixin-java-tools   Source File: WxMpQrCodeTicket.java    License: Apache License 2.0 4 votes vote down vote up
public static WxMpQrCodeTicket fromJson(String json) {
  return WxMpGsonBuilder.INSTANCE.create().fromJson(json, WxMpQrCodeTicket.class);
}
 
Example 23
public static WxMpMemberCardUserInfoResult fromJson(String json) {
  return WxMpGsonBuilder.INSTANCE.create().fromJson(json, WxMpMemberCardUserInfoResult.class);
}
 
Example 24
Source Project: weixin-java-tools   Source File: WxMpMassPreviewMessage.java    License: Apache License 2.0 4 votes vote down vote up
public String toJson() {
  return WxMpGsonBuilder.INSTANCE.create().toJson(this);
}
 
Example 25
Source Project: weixin-java-tools   Source File: WxMpMassTagMessage.java    License: Apache License 2.0 4 votes vote down vote up
public String toJson() {
  return WxMpGsonBuilder.INSTANCE.create().toJson(this);
}
 
Example 26
public static WxDeviceBindDeviceResult fromJson(String json) {
  return WxMpGsonBuilder.create().fromJson(json, WxDeviceBindDeviceResult.class);
}
 
Example 27
Source Project: weixin-java-tools   Source File: WxDeviceOpenIdResult.java    License: Apache License 2.0 4 votes vote down vote up
public static WxDeviceOpenIdResult fromJson(String json) {
  return WxMpGsonBuilder.create().fromJson(json, WxDeviceOpenIdResult.class);
}
 
Example 28
Source Project: weixin-java-tools   Source File: WxDeviceQrCodeResult.java    License: Apache License 2.0 4 votes vote down vote up
public static WxDeviceQrCodeResult fromJson(String json) {
  return WxMpGsonBuilder.INSTANCE.create().fromJson(json, WxDeviceQrCodeResult.class);
}
 
Example 29
Source Project: weixin-java-tools   Source File: WxDeviceBindResult.java    License: Apache License 2.0 4 votes vote down vote up
public static WxDeviceBindResult fromJson(String json) {
  return WxMpGsonBuilder.create().fromJson(json, WxDeviceBindResult.class);
}
 
Example 30
Source Project: weixin-java-tools   Source File: WxMpMassNews.java    License: Apache License 2.0 4 votes vote down vote up
public String toJson() {
  return WxMpGsonBuilder.INSTANCE.create().toJson(this);
}