Android第三方授权(QQ篇)

QQ授权比微信授权相对来说会方便一些

同样需要去官网下载sdk和导入sdk到自己的工程

http://wiki.connect.qq.com/%E7%A7%BB%E5%8A%A8%E5%BA%94%E7%94%A8%E6%8E%A5%E5%85%A5%E6%A6%82%E8%BF%B0

1.初始化sdk,并且调起QQ客户端

Tencent mTencent = Tencent.createInstance(Constants.mQQAppid, context);
mTencent.login(activity, "all", new LoginUiListener());

private static class LoginUiListener implements IUiListener {
        @Override
        public void onComplete(Object response) {
            if (response != null) {
                Log.i(TAG, response.toString());
                JSONObject jsonData = JSON.parseObject(response.toString());
                openId = jsonData.getString("openid");
                mQQToken = mTencent.getQQToken();
                token = mQQToken.getAccessToken();
                mUserInfo = new UserInfo(context, mQQToken);
                mUserInfo.getUserInfo(new QQGetUserInfoListener());
            } else {
                Toast.makeText(context, "QQ登陆获取信息被取消", Toast.LENGTH_LONG)
                        .show();
            }

        }

        @Override
        public void onError(UiError e) {
            Toast.makeText(context, "QQ登陆获取信息错误: " + e.errorMessage,
                    Toast.LENGTH_LONG).show();
        }

        @Override
        public void onCancel() {
            Toast.makeText(context, "QQ登陆获取信息被取消", Toast.LENGTH_LONG).show();
        }
    }
private static class QQGetUserInfoListener implements IUiListener {

        @Override
        public void onComplete(final Object response) {
            // TODO Auto-generated method stub
            // Toast.makeText(context, "QQ登陆获取信息成功", Toast.LENGTH_LONG).show();
            new Thread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    Log.i(TAG, response.toString());
                    JSONObject jsonData = JSON.parseObject(response.toString());
                    //jsonData就保存了QQ用户个人信息                                    }
            }).start();
        }

        @Override
        public void onError(UiError error) {
            // TODO Auto-generated method stub
            Log.e(TAG, error.errorDetail);
        }

        @Override
        public void onCancel() {
            // TODO Auto-generated method stub
            Toast.makeText(context, "QQ登陆获取信息被取消", Toast.LENGTH_LONG).show();
        }

    }

要注意一点,QQ授权是帮我们封装的最好的了,我们不需要自己去调用网络,但是要注意QQ授权的回调都是在UI线程中执行的,所以如果需要在回调中访问自己的服务器,要注意另开线程,以免卡住UI

 

你可能感兴趣的:(android)