package com.qq.open.weibo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.qq.open.common.OpenQqConstants;
import com.qq.open.common.OpenQqUtils;
import com.qq.open.common.json.JSONArray;
import com.qq.open.common.json.JSONException;
import com.qq.open.common.json.JSONObject;
import com.qq.open.weibo.bean.param.WeiBoGetIdoListParamBean;
import com.qq.open.weibo.bean.result.WeiBoGetIdoListResultBean;
import com.qq.open.weibo.bean.result.WeiBoIdoInfoBean;
import com.qq.open.weibo.bean.result.WeiBoTagResultBean;
/**
* 获取登录用户收听的人的列表
*
* @author HaoLiang
*
*/
public class WeiBoGetIdoList {
/** QQ互联工具类 */
private OpenQqUtils oqu = new OpenQqUtils();
/**
* 获取登录用户收听的人的列表
*
* @param paramBean 参数Bean
* @return 登录用户收听的人的列表
* @throws JSONException
* @throws IOException
*/
public WeiBoGetIdoListResultBean getIdoList(WeiBoGetIdoListParamBean paramBean) throws JSONException, IOException {
// 获取登录用户收听的人的列表的接口URL
String interfaceUrl = this.getInterfaceUrl(paramBean);
// 获取接口返回的数据
String jsonData = oqu.doGet(interfaceUrl);
return this.jsonToBean(jsonData);
}
/**
* 获取登录用户收听的人的列表的接口URL
*
* @param paramBean 参数
* @return 接口URL
*/
private String getInterfaceUrl(WeiBoGetIdoListParamBean paramBean) {
// 接口URL
StringBuilder interfaceUrl = new StringBuilder();
// 获取登录用户收听的人的列表 接口URL
interfaceUrl.append(OpenQqConstants.WEIBO_GET_IDOLIST_URL);
// AccessToken
interfaceUrl.append("?access_token=" + paramBean.getAccessToken());
// AppId
interfaceUrl.append("&oauth_consumer_key=" + oqu.getConfigValue("qq.appid"));
// OpenId
interfaceUrl.append("&openid=" + paramBean.getOpenId());
// 请求获取的收听个数
interfaceUrl.append("&reqnum=" + paramBean.getReqNum());
// 请求获取收听列表的起始位置
interfaceUrl.append("&startindex=" + paramBean.getStartIndex());
if (oqu.isNotNull(paramBean.getInstall())) {
// 安装应用标识 1:有应用 2:没有应用 0:忽略
interfaceUrl.append("&install=" + paramBean.getInstall());
}
// 定义API返回的数据格式。
interfaceUrl.append("&format=json");
return interfaceUrl.toString();
}
/**
* 把接口返回的json数据转换成JavaBean
*
* @param jsonStr 接口返回的数据
* @return JavaBean数据
* @throws JSONException
*/
private WeiBoGetIdoListResultBean jsonToBean(String jsonData) throws JSONException {
WeiBoGetIdoListResultBean resultBean = new WeiBoGetIdoListResultBean();
JSONObject jsonObjRoot = new JSONObject(jsonData);
// 接口返回错误的场合
if (jsonObjRoot.getInt("ret") != 0) {
// 设置错误标识为真
resultBean.setErrorFlg(true);
// 设置错误编号
resultBean.setErrorCode(jsonObjRoot.get("errcode").toString());
// 设置错误内容
resultBean.setErrorMes(jsonObjRoot.getString("msg"));
} else {
// 表示是否还有听众信息可以拉取 0:还有听众信息可以拉取 1:已拉取完
JSONObject jsonObjData = new JSONObject(jsonObjRoot.get("data").toString());
resultBean.setHasNext(jsonObjData.get("hasnext").toString());
// 服务器时间戳
resultBean.setTimeStamp(oqu.timeStampToDate(jsonObjData.get("timestamp").toString()));
// 听众详细信息
JSONArray infoJsonObj = jsonObjData.getJSONArray("info");
// 听众详细信息
List<WeiBoIdoInfoBean> infoList = new ArrayList<WeiBoIdoInfoBean>();
// 循环把所有听众信息保存起来
for (int i = 0; i < infoJsonObj.length(); i++) {
WeiBoIdoInfoBean infoBean = new WeiBoIdoInfoBean();
// 单个的听众信息
JSONObject jsonObjTemp = new JSONObject(infoJsonObj.get(i).toString());
// 听众的账户名
infoBean.setName(jsonObjTemp.getString("name"));
// 听众的唯一ID
infoBean.setOpenId(jsonObjTemp.getString("openid"));
// 听众的昵称
infoBean.setNick(jsonObjTemp.getString("nick"));
// 听众头像url
infoBean.setHead(jsonObjTemp.getString("head"));
// 听众性别
// infoBean.setSex(jsonObjTemp.get("sex").toString());
// 听众所在地
infoBean.setLocation(jsonObjTemp.getString("location"));
// 标签
JSONArray tweetArray = jsonObjTemp.getJSONArray("tweet");
JSONObject jsonObjTempTweet = new JSONObject(tweetArray.get(0).toString());
// 听众最近发表的一条微博内容
infoBean.setTweetText(jsonObjTempTweet.getString("text"));
// 听众最近发表的一条微博来源
infoBean.setTweetFrom(jsonObjTempTweet.getString("from"));
// 微博ID,用来唯一标识一条微博
infoBean.setTweetId(jsonObjTempTweet.get("id").toString());
// 发表微博的时间
infoBean.setTweetTime(oqu.timeStampToDate(jsonObjTempTweet.get("timestamp").toString()));
// 听众数
infoBean.setFansnum(jsonObjTemp.get("fansnum").toString());
// 收听的人数
infoBean.setIdolnum(jsonObjTemp.get("idolnum").toString());
// 是否为用户收听的人 0:不是; 1:是
infoBean.setIsidol(jsonObjTemp.get("isidol").toString());
// 听众是否为微博认证用户 0:不是; 1:是
infoBean.setIsvip(jsonObjTemp.get("isvip").toString());
// 听众标签
if (!"null".equals(jsonObjTemp.get("tag").toString())) {
List<WeiBoTagResultBean> tags = new ArrayList<WeiBoTagResultBean>();
// 听众标签
JSONArray tagsArray = jsonObjTemp.getJSONArray("tag");
for (int j = 0; j < tagsArray.length(); j++) {
WeiBoTagResultBean tagsBean = new WeiBoTagResultBean();
// 听众标签
JSONObject tagsObject = new JSONObject(tagsArray.get(j).toString());
// 标签ID
tagsBean.setId(tagsObject.getString("id"));
// 标签名
tagsBean.setName(tagsObject.getString("name"));
tags.add(tagsBean);
}
// 设置用户标签
infoBean.setTabList(tags);
}
// 设置用户相信信息
infoList.add(infoBean);
}
resultBean.setIdoInfoList(infoList);
}
return resultBean;
}
}