friendships/show 作用:获取两个用户之间的详细关注关系情况
新浪文档:http://open.weibo.com/wiki/2/friendships/show
下面是具体的Java实现:
1.构造一个用来存储返回信息的类,并添加如相关的方法
public class FriendRelation extends WeiboResponse implements java.io.Serializable {
// {"source":{"followed_by":true,"following":false,"id":1197161814,"notifications_enabled":false,"screen_name":"李开复"},
// "target":{"followed_by":false,"following":true,"id":1833985035,"notifications_enabled":false,"screen_name":"程序员_张雪峰"}}
private boolean sourceFollowedBy = false;
private boolean sourceFollowing = false;
private String sourceId = new String();
private boolean sourceNotificationsEnabled = false;
private String sourceScreenName = new String();
private boolean targetFollowedBy = false;
private boolean targetFollowing = false;
private String targetId = new String();
private boolean targetNotificationsEnabled = false;
private String targetScreenName = new String();
/*package*/public FriendRelation(JSONObject json) throws WeiboException {
super();
init(json);
}
private void init(JSONObject json) throws WeiboException {
// TODO Auto-generated method stub
if(json!=null) {
try {
JSONObject source = json.getJSONObject("source");
JSONObject target = json.getJSONObject("target");
sourceFollowedBy = source.getBoolean("followed_by");
sourceFollowing = source.getBoolean("following");
sourceId = source.getString("id");
sourceNotificationsEnabled = source.getBoolean("notifications_enabled");
sourceScreenName = source.getString("screen_name");
targetFollowedBy = target.getBoolean("followed_by");
targetFollowing = target.getBoolean("following");
targetId = target.getString("id");
targetNotificationsEnabled = target.getBoolean("notifications_enabled");
targetScreenName = target.getString("screen_name");
} catch (JSONException jsone) {
// TODO Auto-generated catch block
throw new WeiboException(jsone.getMessage() + ":" + json.toString(), jsone);
}
}
}
public void setSourceFollowedBy(boolean sourceFollowedBy) {
this.sourceFollowedBy = sourceFollowedBy;
}
public void setSourceFollowing(boolean sourceFollowing) {
this.sourceFollowing = sourceFollowing;
}
public void setSourceId(String sourceId) {
this.sourceId = sourceId;
}
public void setSourceNotificationsEnabled(boolean sourceNotificationsEnabled) {
this.sourceNotificationsEnabled = sourceNotificationsEnabled;
}
public void setSourceScreenName(String sourceScreenName) {
this.sourceScreenName = sourceScreenName;
}
public void setTargetFollowedBy(boolean targetFollowedBy) {
this.targetFollowedBy = targetFollowedBy;
}
public void setTargetFollowing(boolean targetFollowing) {
this.targetFollowing = targetFollowing;
}
public void setTargetId(String targetId) {
this.targetId = targetId;
}
public void setTargetNotificationsEnabled(boolean targetNotificationsEnabled) {
this.targetNotificationsEnabled = targetNotificationsEnabled;
}
public void setTargetScreenName(String targetScreenName) {
this.targetScreenName = targetScreenName;
}
public boolean getSourceFollowedBy() {
// TODO Auto-generated method stub
return sourceFollowedBy;
}
public boolean getSourceFollowing() {
// TODO Auto-generated method stub
return sourceFollowing;
}
public String getSourceId() {
return sourceId;
}
public boolean getSourceNotificationsEnabled() {
// TODO Auto-generated method stub
return sourceNotificationsEnabled;
}
public String getSourceScreenName() {
return sourceScreenName;
}
public boolean getTargetFollowedBy() {
// TODO Auto-generated method stub
return targetFollowedBy;
}
public boolean getTargetFollowing() {
// TODO Auto-generated method stub
return targetFollowing;
}
public String getTargetId() {
return targetId;
}
public boolean getTargetNotificationsEnabled() {
// TODO Auto-generated method stub
return targetNotificationsEnabled;
}
public String getTargetScreenName() {
return targetScreenName;
}
}
2.建立获取请求数据的方法,最好放在Friendships.java中
public FriendRelation showFriendship(String sourceId,String targetId)
throws WeiboException {
return new FriendRelation(Weibo.client.get(WeiboConfig.getValue("baseURL") +
"friendships/show.json?target_id="+targetId+"&source_id="+sourceId+
"&source="+WeiboConfig.getValue("client_ID")).asJSONObject());
}