Example #1
0
  @Override
  public WeiboUser fetchUser(OAuthTokenPair accessTokenPair) {
    Map<String, String> additionalParams = new HashMap<String, String>();
    additionalParams.put("format", "json");
    WeiboResponse response = null;
    try {
      response = this.protocal.get(USER_INFO_URL, additionalParams, accessTokenPair);
      if (response.isStatusOK()) {
        JSONObject obj =
            JSONObject.fromObject(response.getHttpResponseText()).getJSONObject("data");
        WeiboUser user = new WeiboUser(accessTokenPair);
        user.setNickName(obj.getString("nick"));
        user.setImgUrl(obj.getString("head") + "/180"); // 修正腾讯微博头像url无法访问
        user.setUid(obj.getString("openid"));

        String name = obj.getString("name");
        user.setProfileUrl("http://t.qq.com/" + name + "?preview");

        return user;
      }
    } catch (Exception ex) {
      response.setLocalError(ex);
    }

    log.error("error to get user, resp: " + response);
    return null;
  }
Example #2
0
  private Page<WeiboUser> getPagedWeiboUser(
      String url, OAuthTokenPair accessTokenPair, int page, int pageSize) {
    Map<String, String> additionalParams = new HashMap<String, String>();
    additionalParams.put("format", "json");
    additionalParams.put("reqnum", pageSize + "");
    additionalParams.put("startindex", (page - 1) * pageSize + "");

    WeiboResponse response = this.protocal.get(url, additionalParams, accessTokenPair);
    if (response.isStatusOK()) {
      try {
        JSONObject obj = JSONObject.fromObject(response.getHttpResponseText());
        JSONObject data = obj.getJSONObject("data");
        // 列表为空
        if (data != null && data.isNullObject() == false) {
          JSONArray arr = data.getJSONArray("info");
          Page<WeiboUser> pageUser = new Page<WeiboUser>(page, pageSize);
          List<WeiboUser> users = new ArrayList<WeiboUser>(arr.size());
          pageUser.setContent(users);

          for (int i = 0; i < arr.size(); i++) {
            JSONObject friend = arr.getJSONObject(i);

            WeiboUser weiboFriend = new WeiboUser(null);
            weiboFriend.setNickName(friend.getString("nick"));
            weiboFriend.setImgUrl(friend.getString("head") + "/180"); // 修正腾讯微博头像url无法访问
            weiboFriend.setUid(friend.getString("openid"));

            String name = friend.getString("name");
            weiboFriend.setProfileUrl("http://t.qq.com/" + name + "?preview");

            users.add(weiboFriend);
          }

          return pageUser;
        }
      } catch (Exception e) {
        response.setLocalError(e);
      }
    }

    if (response.isStatusOK() == false)
      log.error("error to fetch paged weibo user, resp: " + response);

    return null;
  }