Search in sources :

Example 6 with JsonReader

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

the class WxCpServiceImpl method tagGetUsers.

@Override
public List<WxCpUser> tagGetUsers(String tagId) throws WxErrorException {
    String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/get?tagid=" + tagId;
    String responseContent = get(url, null);
    JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
    return WxCpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("userlist"), new TypeToken<List<WxCpUser>>() {
    }.getType());
}
Also used : WxCpUser(me.chanjar.weixin.cp.bean.WxCpUser) JsonElement(com.google.gson.JsonElement) TypeToken(com.google.gson.reflect.TypeToken) StringReader(java.io.StringReader) JsonReader(com.google.gson.stream.JsonReader)

Example 7 with JsonReader

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

the class WxCpServiceImpl method tagCreate.

@Override
public String tagCreate(String tagName) throws WxErrorException {
    String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/create";
    JsonObject o = new JsonObject();
    o.addProperty("tagname", tagName);
    String responseContent = post(url, o.toString());
    JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
    return tmpJsonElement.getAsJsonObject().get("tagid").getAsString();
}
Also used : JsonElement(com.google.gson.JsonElement) StringReader(java.io.StringReader) JsonObject(com.google.gson.JsonObject) JsonReader(com.google.gson.stream.JsonReader)

Example 8 with JsonReader

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

the class WxCpServiceImpl method userList.

@Override
public List<WxCpUser> userList(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException {
    String url = "https://qyapi.weixin.qq.com/cgi-bin/user/list?department_id=" + departId;
    String params = "";
    if (fetchChild != null) {
        params += "&fetch_child=" + (fetchChild ? "1" : "0");
    }
    if (status != null) {
        params += "&status=" + status;
    } else {
        params += "&status=0";
    }
    String responseContent = get(url, params);
    JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
    return WxCpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("userlist"), new TypeToken<List<WxCpUser>>() {
    }.getType());
}
Also used : WxCpUser(me.chanjar.weixin.cp.bean.WxCpUser) JsonElement(com.google.gson.JsonElement) TypeToken(com.google.gson.reflect.TypeToken) StringReader(java.io.StringReader) JsonReader(com.google.gson.stream.JsonReader)

Example 9 with JsonReader

use of com.google.gson.stream.JsonReader 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 10 with JsonReader

use of com.google.gson.stream.JsonReader 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)

Aggregations

JsonReader (com.google.gson.stream.JsonReader)95 StringReader (java.io.StringReader)36 JsonElement (com.google.gson.JsonElement)30 Test (org.junit.Test)19 JsonObject (com.google.gson.JsonObject)17 IOException (java.io.IOException)17 InputStreamReader (java.io.InputStreamReader)17 JsonParser (com.google.gson.JsonParser)11 HumanReadableException (com.facebook.buck.util.HumanReadableException)10 Gson (com.google.gson.Gson)9 TypeToken (com.google.gson.reflect.TypeToken)8 JsonWriter (com.google.gson.stream.JsonWriter)8 Map (java.util.Map)7 JsonToken (com.google.gson.stream.JsonToken)6 HashMap (java.util.HashMap)6 InputStream (java.io.InputStream)5 StringWriter (java.io.StringWriter)5 Type (java.lang.reflect.Type)5 ArrayList (java.util.ArrayList)5 BufferedReader (java.io.BufferedReader)4