码迷,mamicode.com
首页 > Windows程序 > 详细

新浪通过API分享 实践

时间:2015-02-06 14:49:27      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:

注:如果集成了百度的Frontia和SinaCoreSDK, 那么SSO会出现包冲突

https://github.com/sinaweibosdk/weibo_android_sdk/issues/56

要先获取API发送的权限

 

 
public class ShareToSinaActivity extends DFActivity {
  
    private WeiboAuthHandler weiboAuthHandler;

    private String shareTitle, shareContent, shareUrlLink, shareImageLink;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        shareTitle = this.getIntent().getExtras().getString("shareTitle");
        shareContent = this.getIntent().getExtras().getString("shareContent");
        shareImageLink = this.getIntent().getExtras().getString("shareImageLink");
        shareUrlLink = this.getIntent().getExtras().getString("shareUrlLink");

    }

    public void sendShareMessageWithAPI() {
        // 获取当前已保存过的 Token
        Oauth2AccessToken mAccessToken = SinaAccessTokenKeeper.readAccessToken(this);
        if (mAccessToken != null && mAccessToken.isSessionValid()) {
            /*
              用于获取微博信息流等操作的API
             */
            StatusesAPI mStatusesAPI = new StatusesAPI(mActivity, AppConstants.KEY_SINA_WEIBO, mAccessToken);

            String strContent = shareTitle + " " + shareContent + " " + shareUrlLink + "(分享自@穿帮时尚)";
            mStatusesAPI.uploadUrlText(strContent, shareImageLink, null, null, null, mListener);
        } else {
            SinaAccessTokenKeeper.clear(mActivity);// 清理新浪Token
            weiboAuthHandler = new WeiboAuthHandler(mActivity);
            weiboAuthHandler.authorize(false);
        }
    }

 
    /**
     * 当 SSO 授权 Activity 退出时,该函数被调用。 当用户登录结束、注册结束时,调用该函数
     *
     * @see {@link android.app.Activity#onActivityResult}
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // requestCode标示请求的标示 resultCode表示有数据
        /**
         * 微博登录结果
         */
        weiboAuthHandler.authorizeCallBack(requestCode, resultCode, data);

    }
    
     /**
     * 微博 OpenAPI 回调接口。
     */
    private RequestListener mListener = new RequestListener() {
        @Override
        public void onComplete(String response) {
            if (!TextUtils.isEmpty(response)) {
                LogUtil.i(tag, response);
                if (response.startsWith("{\"statuses\"")) {
                    // 调用 StatusList#parse 解析字符串成微博列表对象
                    SinaStatusList statuses = SinaStatusList.parse(response);
                    if (statuses != null && statuses.total_number > 0) {
//                        AppUtils.showToast(mActivity, "获取微博信息流成功, 条数: " + statuses.statusList.size());
                    }
                } else if (response.startsWith("{\"created_at\"")) {
                    // 调用 Status#parse 解析字符串成微博对象
                    SinaStatus status = SinaStatus.parse(response);
                    AppUtils.showToast(mActivity, "分享成功");
                } else {
                    log("Sina Share " + response);
                }
            }
            finish();
        }

        @Override
        public void onWeiboException(WeiboException e) {
            LogUtil.e(tag, e.getMessage());
            SinaErrorInfo info = SinaErrorInfo.parse(e.getMessage());
            log("Sina Share " + info.toString());
            finish();
        }
    };
}

 

新浪通过API分享 实践

标签:

原文地址:http://www.cnblogs.com/niray/p/4276906.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!