Search in sources :

Example 11 with JsonElement

use of com.google.gson.JsonElement 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 12 with JsonElement

use of com.google.gson.JsonElement in project weixin-java-tools by chanjarster.

the class WxMpServiceImpl method getJsapiTicket.

public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
    if (forceRefresh) {
        wxMpConfigStorage.expireJsapiTicket();
    }
    if (wxMpConfigStorage.isJsapiTicketExpired()) {
        synchronized (globalJsapiTicketRefreshLock) {
            if (wxMpConfigStorage.isJsapiTicketExpired()) {
                String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi";
                String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
                JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
                JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
                String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
                int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
                wxMpConfigStorage.updateJsapiTicket(jsapiTicket, expiresInSeconds);
            }
        }
    }
    return wxMpConfigStorage.getJsapiTicket();
}
Also used : JsonElement(com.google.gson.JsonElement) StringReader(java.io.StringReader) JsonReader(com.google.gson.stream.JsonReader) JsonObject(com.google.gson.JsonObject) SimpleGetRequestExecutor(me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor)

Example 13 with JsonElement

use of com.google.gson.JsonElement in project weixin-java-tools by chanjarster.

the class WxMpServiceImpl method groupGet.

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());
}
Also used : WxMpGroup(me.chanjar.weixin.mp.bean.WxMpGroup) JsonElement(com.google.gson.JsonElement) TypeToken(com.google.gson.reflect.TypeToken) StringReader(java.io.StringReader) JsonReader(com.google.gson.stream.JsonReader) SimpleGetRequestExecutor(me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor)

Example 14 with JsonElement

use of com.google.gson.JsonElement in project weixin-java-tools by chanjarster.

the class WxMpServiceImpl method userGetGroup.

public long userGetGroup(String openid) throws WxErrorException {
    String url = "https://api.weixin.qq.com/cgi-bin/groups/getid";
    JsonObject o = new JsonObject();
    o.addProperty("openid", openid);
    String responseContent = execute(new SimplePostRequestExecutor(), url, o.toString());
    JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
    return GsonHelper.getAsLong(tmpJsonElement.getAsJsonObject().get("groupid"));
}
Also used : JsonElement(com.google.gson.JsonElement) StringReader(java.io.StringReader) JsonObject(com.google.gson.JsonObject) JsonReader(com.google.gson.stream.JsonReader) SimplePostRequestExecutor(me.chanjar.weixin.common.util.http.SimplePostRequestExecutor)

Example 15 with JsonElement

use of com.google.gson.JsonElement in project weixin-java-tools by chanjarster.

the class WxMpServiceImpl method getUserSummary.

@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());
}
Also used : JsonElement(com.google.gson.JsonElement) TypeToken(com.google.gson.reflect.TypeToken) StringReader(java.io.StringReader) JsonObject(com.google.gson.JsonObject) JsonReader(com.google.gson.stream.JsonReader) WxMpUserSummary(me.chanjar.weixin.mp.bean.result.WxMpUserSummary)

Aggregations

JsonElement (com.google.gson.JsonElement)398 JsonObject (com.google.gson.JsonObject)221 JsonArray (com.google.gson.JsonArray)121 JsonParser (com.google.gson.JsonParser)88 Map (java.util.Map)55 HashMap (java.util.HashMap)49 JsonPrimitive (com.google.gson.JsonPrimitive)48 Gson (com.google.gson.Gson)47 ArrayList (java.util.ArrayList)45 Test (org.testng.annotations.Test)45 IOException (java.io.IOException)40 JsonReader (com.google.gson.stream.JsonReader)30 StringReader (java.io.StringReader)21 Test (org.junit.Test)19 GsonBuilder (com.google.gson.GsonBuilder)18 InputStreamReader (java.io.InputStreamReader)18 Type (java.lang.reflect.Type)17 JsonParseException (com.google.gson.JsonParseException)15 AssetManager (android.content.res.AssetManager)10 List (java.util.List)10