首先是获取OAuth认证,简历一个Oauth类里面执行认证的操作,这个过程我会在下一篇博文里面讲到,那么要想获取登陆用户的所关注用户的id,微博等信息需要对下面这个json地址进行查询!
http://api.t.sina.com.cn/statuses/friends_timeline.json,
关于这个操作的api介绍可以点击下面的链接看下官方的说明!
http://open.weibo.com/wiki/index.php/Statuses/friends_timeline
在用户进行了OAuth认证后,我们就可以开始获取其所关注用户的用户信息及微博了
OAuth oauth=new OAuth();
String url = "http://api.t.sina.com.cn/statuses/friends_timeline.json";
List params=new ArrayList();
params.add(new BasicNameValuePair("source", oauth.consumerKey));
HttpResponse response =oauth.SignRequest(user.getToken(), user.getTokenSecret(), url, params);
if(200==response.getStatusLine().getStatusCode()){
try{
InputStream is=response.getEntity().getContent();//将网页主体以inputStream的形式返回
BufferedReader reader=new BufferedReader(new InputStreamReader(is),4000);//每次读取4000个字节
StringBuffer sb=new StringBuffer((int)response.getEntity().getContentLength());
try{
char[] tmp=new char[1024];
int l;
while((l=reader.read(tmp))!=-1){
//如果没有读完的话
sb.append(tmp,0,l);
}
}finally{
reader.close();
}
String str=sb.toString();
response.getEntity().consumeContent();
JSONArray data=new JSONArray(str);
for(int i=0;i<data.length();i++)
{
//这个是查询登陆用户所有关注人的id,text等信息
JSONObject d=data.getJSONObject(i);
//Log.e("json", "rs:" + d.getString("created_at"));
if(d!=null){
JSONObject u=d.getJSONObject("user");
if(d.has("retweeted_status")){
JSONObject r=d.getJSONObject("retweeted_status");
}
//微博id
id=d.getString("id");
String userId=u.getString("id");
String userName=u.getString("screen_name");
String userIcon=u.getString("profile_image_url");
Log.e("userIcon", userIcon);
String time=d.getString("created_at");
String text=d.getString("text");
String followers_count=d.getString("followers_count");
String description=d.getString("description");
//还可以查询很多信息,这里就不详细写了,下面附一个返回的json大家看下就好了
Boolean haveImg=false;
if(d.has("thumbnail_pic")){
haveImg=true;
//String thumbnail_pic=d.getString("thumbnail_pic");
//Log.e("thumbnail_pic", thumbnail_pic);
}
Date date=new Date(time);
time=date.toGMTString();
if(wbList==null){
wbList=new ArrayList<WeiboInfo>();
}
WeiboInfo w=new WeiboInfo();
w.setId(id);
w.setUser_id(userId);
w.setUserName(userName);
w.setRealeaseTime(time);
w.setUserContent(text);
w.setIsHaveImage(haveImg);
w.setUser_icon(userIcon);
wbList.add(w);
}
}
}catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
然后这样就获取了登陆用户关注用户的信息了,然后下面是来实现关注的功能,这里可以通过被关注用户的用户名和其ID来关注,官方推荐使用ID因为怕避免重复,要想使用ID关注就必须要获得用户的授权,这里我两个方式都讲讲:
首先是用户名,通过对下面这个地址来关注ni要关注用户名得用户,下面那个userName是你要关注微博用户的昵称
http://api.t.sina.com.cn/friendships/create/:userName.format
通过这个地址将返回一个json是你关注用户的信息
List params=new ArrayList();
OAuth oauth=new OAuth();
params.add(new BasicNameValuePair("source", oauth.consumerKey));
HttpResponse response=oauth.SignRequest(user.getToken(),user.getTokenSecret(), "http://api.t.sina.com.cn/friendships/create/"+userName+".format", params);
如果你需要在进行其他的操作,那么类似上面的方法再进行json查询获取信息。
下面是第二种,通过授权获得授权用户的id再进行查询
这个过程也是首先是获取用户id,这个要在OAuth认证里面获得,获取这个id后就类似上面的操作只要将json地址改为:
"http://api.t.sina.com.cn/friendships/create/"+userId()+".json"
就OK了,下面是返回了两段json代码
1、查询登陆用户关注用户的信息:
[ { "created_at" : "Tue Nov 30 16:21:13 +0800 2010", "text" : "转发微博。", "truncated" : false, "retweeted_status" : { "created_at" : "Tue Nov 30 16:05:41 +0800 2010", "text" : "对待事物的出发点、立足点,决定着事物的发展及发展后的结果。这个结果实际也是一种相对而言的成败。世俗间追求这种成败却又调整不好出发点,更是找不到立足点。所以成也好败也好,放下了最好。", "truncated" : false, "in_reply_to_status_id" : "", "annotations" : [ ], "in_reply_to_screen_name" : "", "geo" : null, "user" : { "name" : "归元隆印", "domain" : "", "geo_enabled" : true, "followers_count" : 66710, "statuses_count" : 77, "favourites_count" : 0, "city" : "1", "description" : "心存慈悲 身奉善行 出世入世 修己助人 归元禅寺官方网站:http://www.guiyuanchansi.net", "verified" : true, "id" : 1799833402, "gender" : "m", "friends_count" : 4, "screen_name" : "归元隆印", "allow_all_act_msg" : false, "following" : false, "url" : "http://1", "profile_image_url" : "http://tp3.sinaimg.cn/1799833402/50/1283207796", "created_at" : "Tue Aug 24 00:00:00 +0800 2010", "province" : "42", "location" : "湖北 武汉" }, "favorited" : false, "in_reply_to_user_id" : "", "id" : 3980364843, "source" : "<a href=/"http://t.sina.com.cn/" rel=/"nofollow/">新浪微博</a>" }, "in_reply_to_status_id" : "", "annotations" : [ ], "in_reply_to_screen_name" : "", "geo" : null, "user" : { "name" : "半拉拖鞋", "domain" : "banlatuoxie", "geo_enabled" : true, "followers_count" : 56, "statuses_count" : 333, "favourites_count" : 1, "city" : "5", "description" : "在这里,我只管把话发出去,有没有人理就不管我的事了!", "verified" : false, "id" : 1799824787, "gender" : "m", "friends_count" : 76, "screen_name" : "半拉拖鞋", "allow_all_act_msg" : false, "following" : false, "url" : "http://blog.sina.com.cn/lingdianjingq", "profile_image_url" : "http://tp4.sinaimg.cn/1799824787/50/1289443070/1", "created_at" : "Sun Sep 05 00:00:00 +0800 2010", "province" : "11", "location" : "北京 朝阳区" }, "favorited" : false, "in_reply_to_user_id" : "", "id" : 3980654229, "source" : "<a href=/"http://t.sina.com.cn/" rel=/"nofollow/">新浪微博</a>" }, ... ]