新浪微博邀请好友API

  前两天通过调用官方的SDK完成了授权认证,今天在这基础上调用官网的API实现邀请好友的功能。

  InviteAPI支持已登录用户向自己的微博互粉好友发送私信邀请、礼物。

  调用该接口主要通过以下三个步骤:

  1.创建JSON数据对象,并添加相应参数的数值。

  

JSONObject jsonObject=new JSONObject();
        try
        {
            jsonObject.put(InviteAPI.KEY_TEXT, mEditText.getText());//要回复的私信内容
            jsonObject.put(InviteAPI.KEY_URL,         "http://app.sina.com.cn/appdetail.php?appID=770915");//邀请点击后跳转连接,默认为当前应用连接(可选)
            jsonObject.put(InviteAPI.KEY_INVITE_LOGO, "http://hubimage.com2us.com/hubweb/contents/123_499.jpg");//邀请CARD展示时的目标地址(可选)
        }

 

  2.通过实现RequestListener接口创建API请求结果监听器。

  

public class InviteRequestListener implements RequestListener
    {
        @Override
        public void onComplete(String response) {
            // TODO Auto-generated method stub
            LogUtil.d(TAG, "Invite Response: " + response);

            if (TextUtils.isEmpty(response) || response.contains("error_code")) {
                try {
                    JSONObject obj = new JSONObject(response);
                    String errorMsg = obj.getString("error");
                    String errorCode = obj.getString("error_code");
                    String message = "error_code: " + errorCode + "error_message: " + errorMsg;
                    LogUtil.e(TAG, "Invite Failed: " + message);
                    showToast(false, message);
                    
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                showToast(true, null);
            }
        }

 

  3.创建InviteAPI实例,并调用其sendInvtie方法。

  

//调用OPEN API接口发送邀请
            new InviteAPI(accesston).sendInvite(uid, jsonObject, mInviteRequestListener);

posted on 2014-07-20 16:09  小太阳550  阅读(802)  评论(0)    收藏  举报

导航