QQ第三方登录分享

1、Bean 类

private int ret;
private String msg;
private int is_lost;
private String nickname;
private String gender;
private String province;
private String city;
private String figureurl;
private String figureurl_1;
private String figureurl_2;
private String figureurl_qq_1;
private String figureurl_qq_2;
private String is_yellow_vip;
private String vip;
private String yellow_vip_level;
private String level;
private String is_yellow_year_vip;

public int getRet() {
    return ret;
}

public void setRet(int ret) {
    this.ret = ret;
}

public String getMsg() {
    return msg;
}

public void setMsg(String msg) {
    this.msg = msg;
}

public int getIs_lost() {
    return is_lost;
}

public void setIs_lost(int is_lost) {
    this.is_lost = is_lost;
}

public String getNickname() {
    return nickname;
}

public void setNickname(String nickname) {
    this.nickname = nickname;
}

public String getGender() {
    return gender;
}

public void setGender(String gender) {
    this.gender = gender;
}

public String getProvince() {
    return province;
}

public void setProvince(String province) {
    this.province = province;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getFigureurl() {
    return figureurl;
}

public void setFigureurl(String figureurl) {
    this.figureurl = figureurl;
}

public String getFigureurl_1() {
    return figureurl_1;
}

public void setFigureurl_1(String figureurl_1) {
    this.figureurl_1 = figureurl_1;
}

public String getFigureurl_2() {
    return figureurl_2;
}

public void setFigureurl_2(String figureurl_2) {
    this.figureurl_2 = figureurl_2;
}

public String getFigureurl_qq_1() {
    return figureurl_qq_1;
}

public void setFigureurl_qq_1(String figureurl_qq_1) {
    this.figureurl_qq_1 = figureurl_qq_1;
}

public String getFigureurl_qq_2() {
    return figureurl_qq_2;
}

public void setFigureurl_qq_2(String figureurl_qq_2) {
    this.figureurl_qq_2 = figureurl_qq_2;
}

public String getIs_yellow_vip() {
    return is_yellow_vip;
}

public void setIs_yellow_vip(String is_yellow_vip) {
    this.is_yellow_vip = is_yellow_vip;
}

public String getVip() {
    return vip;
}

public void setVip(String vip) {
    this.vip = vip;
}

public String getYellow_vip_level() {
    return yellow_vip_level;
}

public void setYellow_vip_level(String yellow_vip_level) {
    this.yellow_vip_level = yellow_vip_level;
}

public String getLevel() {
    return level;
}

public void setLevel(String level) {
    this.level = level;
}

public String getIs_yellow_year_vip() {
    return is_yellow_year_vip;
}

public void setIs_yellow_year_vip(String is_yellow_year_vip) {
    this.is_yellow_year_vip = is_yellow_year_vip;
}
 2、jar包

D:\Android_Studio\依赖/qq第三方

3、AndroidMainFest

    android:name="com.tencent.open.yyb.AppbarActivity"
    android:configChanges="orientation|keyboardHidden"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />
android:name="com.tencent.connect.avatar.ImageActivity" />
    android:name="com.tencent.connect.common.AssistActivity"
    android:configChanges="orientation|keyboardHidden"
    android:screenOrientation="behind"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />
    android:name="com.tencent.tauth.AuthActivity"
    android:launchMode="singleTask"
    android:noHistory="true" >
    
        android:name="android.intent.action.VIEW" />
        android:name="android.intent.category.DEFAULT" />
        android:name="android.intent.category.BROWSABLE" />
        android:scheme="tencent1106062414" />
        
    

4、MainActivity

 

TextView tv_name;
        TextView tv_content;
        ImageView imageView;
        private UserInfo mInfo;
        public static Tencent mTencent;
        public static String mAppid="1106062414";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
        }
        private void initView()
        {
//            tv_name= (TextView) findViewById(R.id.name);
//            tv_content= (TextView) findViewById(R.id.content);
//            imageView= (ImageView) findViewById(R.id.user_logo);
//            findViewById(R.id.new_login_btn).setOnClickListener(this);
//            findViewById(R.id.new_login_close).setOnClickListener(this);
//            findViewById(R.id.new_login_shareqq).setOnClickListener(this);
//            findViewById(R.id.new_login_shareqzone).setOnClickListener(this);
            if (mTencent == null) {
            mTencent = Tencent.createInstance(mAppid, this);
        }
        }
        
        
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.new_login_btn:
                    onClickLogin();
                    break;
                case R.id.new_login_close:
                    mTencent.logout(MainActivity.this);//注销登录
                    break;
                case R.id.new_login_shareqq:
                    onClickShare();
                    break;
                case R.id.new_login_shareqzone:
                    shareToQQzone();
                    break;
            }
        }

        /**
         * 继承的到BaseUiListener得到doComplete()的方法信息
         */
        IUiListener loginListener = new BaseUiListener() {
            @Override
            protected void doComplete(JSONObject values) {//得到用户的ID  和签名等信息  用来得到用户信息
                Log.i("lkei",values.toString());
                initOpenidAndToken(values);
                updateUserInfo();
            }
        };
        /***
         * QQ平台返回返回数据个体 loginListener的values
         */
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == Constants.REQUEST_LOGIN ||
                    requestCode == Constants.REQUEST_APPBAR) {
                Tencent.onActivityResultData(requestCode,resultCode,data,loginListener);
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
        private class BaseUiListener implements IUiListener {
            @Override
            public void onComplete(Object response) {
                if (null == response) {
                    Toast.makeText(MainActivity.this, "登录失败",Toast.LENGTH_LONG).show();
                    return;
                }
                JSONObject jsonResponse = (JSONObject) response;
                if (null != jsonResponse && jsonResponse.length() == 0) {
                    Toast.makeText(MainActivity.this, "登录失败",Toast.LENGTH_LONG).show();
                    return;
                }
                Toast.makeText(MainActivity.this, "登录成功",Toast.LENGTH_LONG).show();
                doComplete((JSONObject)response);
            }

            protected void doComplete(JSONObject values) {

            }
            @Override
            public void onError(UiError e) {
                //登录错误
            }

            @Override
            public void onCancel() {
                // 运行完成
            }
        }
        /**
         * 获取登录QQ腾讯平台的权限信息(用于访问QQ用户信息)
         * @param jsonObject
         */
        public static void initOpenidAndToken(JSONObject jsonObject) {
            try {
                String token = jsonObject.getString(Constants.PARAM_ACCESS_TOKEN);
                String expires = jsonObject.getString(Constants.PARAM_EXPIRES_IN);
                String openId = jsonObject.getString(Constants.PARAM_OPEN_ID);
                if (!TextUtils.isEmpty(token) && !TextUtils.isEmpty(expires)
                        && !TextUtils.isEmpty(openId)) {
                    mTencent.setAccessToken(token, expires);
                    mTencent.setOpenId(openId);
                }
            } catch(Exception e) {
            }
        }
        private void onClickLogin() {
            if (!mTencent.isSessionValid()) {
                mTencent.login(this, "all", loginListener);
            }
        }
        private void updateUserInfo() {
            if (mTencent != null && mTencent.isSessionValid()) {
                IUiListener listener = new IUiListener() {
                    @Override
                    public void onError(UiError e) {
                    }
                    @Override
                    public void onComplete(final Object response) {
                        Message msg = new Message();
                        msg.obj = response;
                        Log.i("tag", response.toString());
                        msg.what = 0;
                        mHandler.sendMessage(msg);
                    }
                    @Override
                    public void onCancel() {
                    }
                };
                mInfo = new UserInfo(this, mTencent.getQQToken());
                mInfo.getUserInfo(listener);

            }
        }
        Handler mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 0) {
                    JSONObject response = (JSONObject) msg.obj;
                    if (response.has("nickname")) {
                        try {
                            Gson gson=new Gson();
                            User user=gson.fromJson(response.toString(),User.class);
                            if (user!=null) {
                                tv_name.setText("昵称:"+user.getNickname()+"  性别:"+user.getGender()+"  地址:"+user.getProvince()+user.getCity());
                                tv_content.setText("头像路径:"+user.getFigureurl_qq_2());
                                Picasso.with(MainActivity.this).load(response.getString("figureurl_qq_2")).into(imageView);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        };

        //qq分享
        private void onClickShare() {
            final Bundle params = new Bundle();
            params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE,
                    QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
            params.putString(QQShare.SHARE_TO_QQ_TITLE, "你喜欢看美女吗?");
            params.putString(QQShare.SHARE_TO_QQ_SUMMARY, "那就点开吧");
            params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,
                    "https://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gbk&word=%C3%C0%C5%AE&fr=ala&ala=1&alatpl=adress&pos=0&hs=2&xthttps=111111");
            params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,
                    "https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=false&word=%E7%BE%8E%E5%A5%B3&step_word=&hs=2&pn=15&spn=0&di=40114350350&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=undefined&cs=3448661152%2C313512508&os=2267095064%2C2244125349&simid=0%2C0&adpicid=0&lpn=0&ln=3940&fr=&fmq=1522737336066_R&fm=&ic=undefined&s=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&ist=&jit=&cg=girl&bdtype=11&oriquery=&objurl=http%3A%2F%2Fimage5.tuku.cn%2Fpic%2Fwallpaper%2Fmeinv%2Fmeikongxingganmeinvxiezhenbizhi%2F005.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3Bp7h7_z%26e3BvgAzdH3Fowssrwrj6nb80_aac_z%26e3Bwfrx&gsm=0&rpstart=0&rpnum=0&islist=&querylist=");
            params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "来自王奎的分享");
            params.putString(QQShare.SHARE_TO_QQ_EXT_INT, "其他附加功能");
            mTencent.shareToQQ(MainActivity.this, params, new BaseUiListener1());
        }
        //回调接口  (成功和失败的相关操作)
        private class BaseUiListener1 implements IUiListener {
            @Override
            public void onComplete(Object response) {
                doComplete(response);
            }

            protected void doComplete(Object values) {
            }

            @Override
            public void onError(UiError e) {
            }

            @Override
            public void onCancel() {
            }
        }

        @SuppressWarnings("unused")
        private void shareToQQzone() {
            try {
                final Bundle params = new Bundle();
                params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE,
                        QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
                params.putString(QzoneShare.SHARE_TO_QQ_TITLE, "小小太阳");
                params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, "sss");
                params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL,
                        "http://blog.csdn.net/DickyQie/article/list/1");
                ArrayList imageUrls = new ArrayList();
                imageUrls.add("http://media-cdn.tripadvisor.com/media/photo-s/01/3e/05/40/the-sandbar-that-links.jpg");
                params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imageUrls);
                params.putInt(QzoneShare.SHARE_TO_QQ_EXT_INT,
                        QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN);
                Tencent mTencent = Tencent.createInstance("1106062414",
                        MainActivity.this);
                mTencent.shareToQzone(MainActivity.this, params,
                        new BaseUiListener1());
            } catch (Exception e) {
            }

}

4、布局


你可能感兴趣的:(个人学习)