Search in sources :

Example 6 with JsonElement

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

the class WxCpServiceImpl method getJsapiTicket.

public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
    if (forceRefresh) {
        wxCpConfigStorage.expireJsapiTicket();
    }
    if (wxCpConfigStorage.isJsapiTicketExpired()) {
        synchronized (globalJsapiTicketRefreshLock) {
            if (wxCpConfigStorage.isJsapiTicketExpired()) {
                String url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket";
                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();
                wxCpConfigStorage.updateJsapiTicket(jsapiTicket, expiresInSeconds);
            }
        }
    }
    return wxCpConfigStorage.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 7 with JsonElement

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

the class WxCpServiceImpl method getCallbackIp.

@Override
public String[] getCallbackIp() throws WxErrorException {
    String url = "https://qyapi.weixin.qq.com/cgi-bin/getcallbackip";
    String responseContent = get(url, null);
    JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
    JsonArray jsonArray = tmpJsonElement.getAsJsonObject().get("ip_list").getAsJsonArray();
    String[] ips = new String[jsonArray.size()];
    for (int i = 0; i < jsonArray.size(); i++) {
        ips[i] = jsonArray.get(i).getAsString();
    }
    return ips;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) StringReader(java.io.StringReader) JsonReader(com.google.gson.stream.JsonReader)

Example 8 with JsonElement

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

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

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

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