Exemplo n.º 1
1
  public UserListBean getUserList() throws WeiboException {

    String url = WeiBoURLs.USERS_SEARCH;

    Map<String, String> map = new HashMap<String, String>();
    map.put("access_token", access_token);
    map.put("count", count);
    map.put("page", page);
    map.put("q", q);

    String jsonData = null;

    jsonData = HttpUtility.getInstance().executeNormalTask(HttpMethod.Get, url, map);

    Gson gson = new Gson();

    UserListBean value = null;
    try {
      value = gson.fromJson(jsonData, UserListBean.class);
    } catch (JsonSyntaxException e) {

      AppLoggerUtils.e(e.getMessage());
    }

    return value;
  }
Exemplo n.º 2
0
  public List<String> getInfo() throws WeiboException {

    String url = WeiBoURLs.GROUP_MEMBER_LIST;

    Map<String, String> map = new HashMap<String, String>();
    map.put("access_token", access_token);
    map.put("uids", uids);

    String jsonData = HttpUtility.getInstance().executeNormalTask(HttpMethod.Get, url, map);

    Gson gson = new Gson();

    List<GroupUser> value = null;
    try {
      value = gson.fromJson(jsonData, new TypeToken<List<GroupUser>>() {}.getType());
    } catch (JsonSyntaxException e) {
      AppLoggerUtils.e(e.getMessage());
    }

    if (value != null && value.size() > 0) {
      GroupUser user = value.get(0);
      List<String> ids = new ArrayList<String>();
      for (GroupBean b : user.lists) {
        ids.add(b.getIdstr());
      }
      return ids;
    }

    return null;
  }
Exemplo n.º 3
0
  public MessageListBean getGSONMsgList() {

    String json = getMsgListJson();
    Gson gson = new Gson();

    MessageListBean value = null;

    try {
      value = gson.fromJson(json, MessageListBean.class);
    } catch (JsonSyntaxException e) {
      ActivityUtils.showTips("发生错误,请重刷");
      AppLogger.e(e.getMessage().toString());
    }

    if (value != null) {
      List<WeiboMsgBean> msgList = value.getStatuses();

      Iterator<WeiboMsgBean> iterator = msgList.iterator();

      while (iterator.hasNext()) {

        WeiboMsgBean msg = iterator.next();
        if (msg.getUser() == null) {
          iterator.remove();
        }
      }
    }
    return value;
  }
 /** 把Json格式的字符串转换成实体类型的方法. 注意:实体类中的变量需要用@SerializedName注释 */
 public final <T> T get(String value, Class<T> classOfT) {
   try {
     Gson gson =
         new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
     return gson.fromJson(value, classOfT);
   } catch (JsonSyntaxException exception) {
     Log.d("JsonSyntaxException", exception.getMessage());
   }
   return null;
 }
Exemplo n.º 5
0
  private static String[] deserializeJson(final String value) {
    log.debug("Deserialization JSON data");

    try {
      return new Gson().fromJson(value, String[].class);
    } catch (final JsonSyntaxException e) {
      log.warn("Deserialization error, using string as is: {}", e.getMessage());
      return new String[] {value};
    }
  }
  public static WrappedStack createFromJson(String jsonWrappedObject) throws JsonParseException {
    try {
      return gsonSerializer.fromJson(jsonWrappedObject, WrappedStack.class);
    } catch (JsonSyntaxException exception) {
      LogHelper.severe(exception.getMessage());
    } catch (JsonParseException exception) {
      LogHelper.severe(exception.getMessage());
    }

    return null;
  }
Exemplo n.º 7
0
  @Override
  public void ImportWarningCategories() {
    List<WarningCategory> warningCategories = new ArrayList<WarningCategory>();
    try {
      Reader reader =
          new InputStreamReader(
              AdminServiceImpl.class
                  .getClassLoader()
                  .getResourceAsStream("mongo/warningCategories.json"));
      warningCategories =
          new Gson().fromJson(reader, new TypeToken<List<WarningCategory>>() {}.getType());

    } catch (JsonSyntaxException e) {
      log.error(e.getMessage(), e);
      throw new MedCheckerException(e.getMessage());
    } catch (JsonIOException e) {
      log.error(e.getMessage(), e);
      throw new MedCheckerException(e.getMessage());
    }

    warningRepo.insert(warningCategories);
  }
Exemplo n.º 8
0
  /**
   * Adapter for spotify's search api
   *
   * @param query - to match tracks
   * @param page - what page with results to see
   * @return Tracks matching query
   * @throws SpotifyApiException - Error contacting spotify
   */
  private SpotifyTrackSearchContainer searchSpotify(String query, int page)
      throws SpotifyApiException {
    RestTemplate rest = new RestTemplate();
    SpotifyTrackSearchContainer response = null;

    try {
      logger.debug("Searching for spotify-songs with query " + query);
      String jsonResponse =
          rest.getForObject(
              "http://ws.spotify.com/search/1/track.json?q=" + query + "&page=" + page,
              String.class);
      response = gson.fromJson(jsonResponse, SpotifyTrackSearchContainer.class);

      for (int i = response.getTracks().size() - 1;
          i >= 0;
          i--) { // remove songs from result that cannot be played in norway
        if (!isPlayable(response.getTracks().get(i))) {
          logger.debug(
              "Song "
                  + response.getTracks().get(i).getName()
                  + " is not playable in Norway, ignoring");
          response.getTracks().remove(i);
        }
      }
    } catch (RestClientException e) {
      logger.error(
          "Exception while searching for spotifysongs matching "
              + query
              + " error: "
              + e.getMessage());
      throw new SpotifyApiException(e.getMessage());
    } catch (JsonSyntaxException e) {
      logger.error(
          "Error reading response from spotify: " + response + " error: " + e.getMessage());
      throw new SpotifyApiException(e.getMessage());
    }
    return response;
  }
 Object throwExceptions(Type type, String jsonFile) {
   try {
     Gson gson = new Gson();
     Thread.sleep(2000);
     return gson.fromJson(readFileFromAssets(jsonFile, context), type);
   } catch (JsonSyntaxException e) {
     Log.e(ERROR_API, jsonFile + " contains a syntax error " + e.getMessage());
   } catch (IOException e) {
     Log.e(ERROR_API, jsonFile + " could not be found");
   } catch (InterruptedException e) {
     Log.e(ERROR_API, e.getMessage());
   }
   return new Object();
 }
Exemplo n.º 10
0
 protected void process(HttpServletRequest request, HttpServletResponse response)
     throws IOException {
   response.setContentType("application/json");
   response.setCharacterEncoding("UTF-8");
   response.setStatus(200);
   JsonObject res;
   try {
     res = getResponse(request);
     response.getWriter().print(res);
     response.getWriter().close();
   } catch (JsonSyntaxException e) {
     throw new GridException(e.getMessage());
   }
 }
Exemplo n.º 11
0
  public List<MessageReCmtCountBean> get() throws WeiboException {

    String json = getJson();

    List<MessageReCmtCountBean> value = null;
    try {
      value = new Gson().fromJson(json, new TypeToken<List<MessageReCmtCountBean>>() {}.getType());
    } catch (JsonSyntaxException e) {

      AppLogger.e(e.getMessage());
    }

    return value;
  }
Exemplo n.º 12
0
 private UserCacheRecord read(StorageKey key) throws LocalDBException {
   final String jsonValue = localDB.get(DB, key.getKey());
   if (jsonValue != null && !jsonValue.isEmpty()) {
     try {
       return JsonUtil.deserialize(jsonValue, UserCacheRecord.class);
     } catch (JsonSyntaxException e) {
       LOGGER.error(
           "error reading record from cache store for key="
               + key.getKey()
               + ", error: "
               + e.getMessage());
       localDB.remove(DB, key.getKey());
     }
   }
   return null;
 }
Exemplo n.º 13
0
  public boolean destroy() throws WeiboException {
    String url = URLHelper.STATUSES_DESTROY;
    Map<String, String> map = new HashMap<String, String>();
    map.put("access_token", access_token);
    map.put("id", id);

    String jsonData = HttpUtility.getInstance().executeNormalTask(HttpMethod.Post, url, map);
    Gson gson = new Gson();

    try {
      MessageBean value = gson.fromJson(jsonData, MessageBean.class);
    } catch (JsonSyntaxException e) {
      AppLogger.e(e.getMessage());
      return false;
    }

    return true;
  }
Exemplo n.º 14
0
 public static <R> R parseJson(okhttp3.Response response, Type bodyType)
     throws HasuraJsonException {
   int code = response.code();
   try {
     String rawBody = response.body().string();
     System.out.println(rawBody);
     return gson.fromJson(rawBody, bodyType);
   } catch (JsonSyntaxException e) {
     String msg =
         "FATAL : JSON strucutre not as expected. Schema changed maybe? : " + e.getMessage();
     throw new HasuraJsonException(code, msg, e);
   } catch (JsonParseException e) {
     String msg = "FATAL : Server didn't return vaild JSON : " + e.getMessage();
     throw new HasuraJsonException(code, msg, e);
   } catch (IOException e) {
     String msg = "FATAL : Decoding response body failed : " + e.getMessage();
     throw new HasuraJsonException(code, msg, e);
   }
 }
Exemplo n.º 15
0
  public MessageListBean getGSONMsgList() throws WeiboException {

    String json = getMsgListJson();
    Gson gson = new Gson();

    MessageListBean value = null;
    try {
      value = gson.fromJson(json, MessageListBean.class);
    } catch (JsonSyntaxException e) {

      AppLogger.e(e.getMessage());
      return null;
    }
    if (value != null && value.getItemList().size() > 0) {
      TimeLineUtility.filterMessage(value);
    }

    return value;
  }
Exemplo n.º 16
0
  public boolean destroy() throws WeiboException {

    String url = URLHelper.GROUP_DESTROY;

    Map<String, String> map = new HashMap<String, String>();
    map.put("access_token", access_token);
    map.put("list_id", list_id);

    String jsonData = HttpUtility.getInstance().executeNormalTask(HttpMethod.Post, url, map);

    Gson gson = new Gson();

    Result value = null;
    try {
      value = gson.fromJson(jsonData, Result.class);
    } catch (JsonSyntaxException e) {
      AppLogger.e(e.getMessage());
    }

    return (value != null);
  }
Exemplo n.º 17
0
  public SearchStatusListBean getStatusList() throws WeiboException {

    String url = WeiBoURLs.STATUSES_SEARCH;

    Map<String, String> map = new HashMap<String, String>();
    map.put("access_token", access_token);
    map.put("count", count);
    map.put("page", page);
    map.put("q", q);

    String jsonData = null;

    jsonData = HttpUtility.getInstance().executeNormalTask(HttpMethod.Get, url, map);

    Gson gson = new Gson();

    SearchStatusListBean value = null;
    try {
      value = gson.fromJson(jsonData, SearchStatusListBean.class);
      List<MessageBean> list = value.getItemList();

      Iterator<MessageBean> iterator = list.iterator();
      while (iterator.hasNext()) {
        MessageBean msg = iterator.next();
        // message is deleted by sina
        if (msg.getUser() == null) {
          iterator.remove();
        } else {
          msg.getListViewSpannableString();
          TimeUtility.dealMills(msg);
        }
      }
    } catch (JsonSyntaxException e) {

      AppLoggerUtils.e(e.getMessage());
    }

    return value;
  }
Exemplo n.º 18
0
  public <T> T get(String path, Class<T> c) {
    Log.d(TAG, "get " + path); // NON-NLS

    File f = new File(getFullCachePath(path));
    if (!f.exists()) {
      Log.w(TAG, "file: " + f.toString() + " not exists"); // NON-NLS
      return null;
    }

    T object;
    try {
      StringBuffer fileData = new StringBuffer();
      BufferedReader reader = new BufferedReader(new FileReader(f));
      char[] buf = new char[1024];
      int numRead = 0;
      while ((numRead = reader.read(buf)) != -1) {
        String readData = String.valueOf(buf, 0, numRead);
        fileData.append(readData);
      }
      reader.close();
      String json = fileData.toString();
      Log.d(TAG, "cache: " + json); // NON-NLS

      object = getGson().fromJson(json, c);
    } catch (FileNotFoundException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (StreamCorruptedException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (IOException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (JsonSyntaxException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (IllegalArgumentException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (RuntimeException e) {
      // не позволим кэшу убить нашу программу
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (Exception e) {
      // не позволим кэшу убить нашу программу
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    }

    if (object.getClass().toString().equals(c.toString())) return object;
    else return null;
  }
Exemplo n.º 19
0
  /**
   * @param path
   * @param t
   * @return
   */
  public List<?> getList(String path, java.lang.reflect.Type t) {
    Log.d(TAG, "get list " + path); // NON-NLS

    File f = new File(getFullCachePath(path));
    if (!f.exists()) {
      Log.w(TAG, "file: " + f.toString() + " not exists"); // NON-NLS
      return null;
    }

    List<?> list;
    try {
      StringBuffer fileData = new StringBuffer();
      BufferedReader reader = new BufferedReader(new FileReader(f));
      char[] buf = new char[1024];
      int numRead = 0;
      while ((numRead = reader.read(buf)) != -1) {
        String readData = String.valueOf(buf, 0, numRead);
        fileData.append(readData);
      }
      reader.close();
      String json = fileData.toString();
      Log.d(TAG, "cache: " + json); // NON-NLS

      list = gson.fromJson(json, t);
    } catch (FileNotFoundException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (StreamCorruptedException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (IOException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (JsonSyntaxException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (IllegalArgumentException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (RuntimeException e) {
      // не позволим кэшу убить нашу программу
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (Exception e) {
      // не позволим кэшу убить нашу программу
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    }

    return list;
  }