新浪微博API friendships/show的Java实现

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());
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值