Android Weibo QQ Login Authorization function instance

Source: Internet
Author: User

Recently started to do the company's small projects, including Weibo and QQ user login, here to record, the other QQ documents very disgusting, despise a!

Weibo authorized Login:

Go to the Weibo open platform application, fill in the package name and signature of the Android app, and then download the Import SDK.

(Many fool tutorials have these things in the registration process, but if you do not make it easy to do the development, micro-blog basically look at the SDK documents and samples basically no problem)

Directly on the code, is actually very simple:

Weibo login related Operations Zhangyue 20141201/////////////////////// Private Weiboauth weiboauth;private Oauth2accesstoken Weiboaccesstoken; Private Ssohandler ssohandler;private usersapi usersapi;/** * Login button event, enter the authorization login process * * @param view */public void Onweibologin ( View view) {Weiboauth = new Weiboauth (this, Constants.app_key, Constants.redirect_url, constants.scope);// SSO Secure Login Ssohandler = new Ssohandler (this, weiboauth); Ssohandler.authorize (new Authlistener ());} /** * Authorized Login Monitoring * * @author Zhang.yue * @date December 1, 2014 */class Authlistener implements Weiboauthlistener {@Overridepublic void OnCancel () {} @Overridepublic void OnComplete (Bundle arg0) {//Get information such as uid,token Weiboaccesstoken = Oauth2accesstoken.parseaccesstoken (arg0); if (Weiboaccesstoken.issessionvalid ()) {//Login successful, get user information Usersapi = new Usersapi (Weiboaccesstoken); Long uid = Long.parselong (Weiboaccesstoken.getuid ()); Usersapi.show (UID, Requestlistener );} ElSE {//Login failed System.out.println (arg0.get ("Code"));}} @Overridepublic void Onweiboexception (Weiboexception arg0) {}}/** * Listen for user information */requestlistener Requestlistener = new Requ Estlistener () {@Overridepublic void onweiboexception (Weiboexception arg0) {} @Overridepublic void OnComplete (String ARG0) {//Get user Information jsonSystem.out.println (arg0);}};

QQ Authorized Login:

The same to QQ Internet Platform application fill information, but the document is very disgusting QQ, first look at the code, one will say:

QQ Login Related operation Zhangyue 20141201/////////////////////// Private Tencent tencent;/** * Login Button event, enter the authorization login process * * @param view */public void Onqqlog In (view view) {Tencent = Tencent.createinstance (Constants.qq_app_key, Getapplicationcontext ()); Tencent.login (This, Constants.qq_scope, New Baseuilistener ("Login");} /** * UI Class Wrapper interface Listener * @author Zhang.yue * @date December 1, 2014 */class Baseuilistener implements iuilistener {private String scope ;p ublic Baseuilistener (String scope) {this.scope = scope;} @Overridepublic void OnCancel () {} @Overridepublic void OnError (Uierror arg0) {System.err.println (arg0.errorcode);} @Overridepublic void OnComplete (Object arg0) {if (This.scope = = "Login") {//Authorization login succeeded, get to Uid,tokensystem.out.println (arg0 );//obtain user information userinfo userinfo = new UserInfo (Loginmainactivity.this, Tencent.getqqtoken ()); Userinfo.getuserinfo (new Baseuilistener ("Get_user_info"));} else if (this.scope.equals ("Get_user_Info ")) {//Get user Information JSON format SYSTEM.OUT.PRINTLN (ARG0);}}} /** * Non-encapsulated class interface monitoring Tencent.request (synchronous) Tencent.requestasync (asynchronous) * @author Zhang.yue * @date December 1, 2014 *///class baseapilist Ener implements Irequestlistener {////@Override//public void OnComplete (Jsonobject arg0) {//system.out.println (arg0) ;//}////@Override//public void onconnecttimeoutexception (Connecttimeoutexception arg0) {//}////@Override//public void Onhttpstatusexception (Httpstatusexception arg0) {//}////@Override//public void onioexception (IOException arg0) { }////@Override//public void onjsonexception (Jsonexception arg0) {//}////@Override//public void Onmalformedurlexception (malformedurlexception arg0) {//}////@Override//public void Onnetworkunavailableexception ( Networkunavailableexception arg0) {//}////@Override//public void Onsockettimeoutexception (sockettimeoutexception arg0) {//}////@Override//public void onunknowexception (Exception arg0) {//}////}
In the QQ document, get the user information is written like this:

/** * Get user information */private void Onclickuserinfo () {        mtencent.requestasync (constants.graph_user_info, NULL,                    Constants.http_get, New Baseapilistener ("Get_user_info", false), NULL);}

The result constants.graph_user_info this at all, in fact the new has been replaced by the userinfo kind of way to acquire. I still have to import the example to see it.


Also, Activityresult to rewrite:

@Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult ( Requestcode, ResultCode, data); if (Ssohandler! = null) {Ssohandler.authorizecallback (Requestcode, ResultCode, data);} if (Tencent! = null) {Tencent.onactivityresult (Requestcode, ResultCode, data);}}
This allows the authorization and user information to be received, the SDK version of Weibo is 2.5,qq is 2.2.

------------------------------------------------

My Independent blog: Accommodating Kid- http://www.zyblog.net/

This article link: http://www.zyblog.net/post-177.html

fitness-it People fitness Guide: http://www.jianshenchao.com

Welcome reprint, Reproduced Please indicate the source of this article.


Android Weibo QQ Login Authorization function instance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.