Implementation of authorized user login in Sina Weibo Development

Source: Internet
Author: User
Tags oauth

When an authorized user starts an application again, we need to read it from the database.Authorized user informationAllows the user to select the user name to log on. Read the token and tokenSecret from the database and initialize the accessToken to get Weibo data!

Logon interface:

The layout won't go to the code, which is relatively simple and directly goes to the Code:

Package COM. cloay. weibo. ui; import Java. util. hashmap; import weibo4android. weibo; import weibo4android. HTTP. accesstoken; import COM. cloay. weibo. r; import COM. cloay. weibo. bean. userinfo; import COM. cloay. weibo. constants. oauthconstant; import COM. cloay. weibo. DB. datahelper; import COM. cloay. weibo. service. mainservice; import COM. cloay. weibo. service. task; import COM. cloay. weibo. utils. alertdlgutil; import COM. cloay. weibo. utils. checknetutil; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. menu; import android. view. menuitem; import android. view. view; import android. view. view. onclicklistener; import android. view. window; import android. widget. button; import android. widget. edittext; import android. widget. imageview;/*** authorized user logon interface * @ author cloay * 2012-2-20*08:42:23 */public class loginactivity extends activity {private datahelper; // The database private imageview usericon that stores user information; // the user profile private edittext username; // The user nickname private button login; // The logon button private userinfo; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); requestwindowfeature (window. feature_no_title); setcontentview (R. layout. login); mainservice. allactivity. add (this); system. setproperty ("weibo4j. oauth. consumerkey ", Weibo. consumer_key); system. setproperty ("weibo4j. oauth. consumersecret ", Weibo. consumer_secret); datahelper = new datahelper (this); userinfo = datahelper. getuserlist (true ). get (0); // read authorized user information from the database. out. println ("------ userinfo:" + userinfo); // generate accesstokenaccesstoken accesstoken = new accesstoken (userinfo. gettoken (), userinfo. getsecret (); oauthconstant. getinstance (). setaccesstoken (accesstoken); usericon = (imageview) findviewbyid (R. id. user_icon); usericon. setimagebitmap (userinfo. getdrawable (); // set the user profile username = (edittext) findviewbyid (R. id. user_edit); username. settext (userinfo. getname (); // username login = (button) findviewbyid (R. id. login); login. setonclicklistener (New onclicklistener () {// click Login to directly go to the home page @ overridepublic void onclick (view v) {If (checknetutil. checknet (loginactivity. this) {// check whether the Network is available intent = new intent (loginactivity. this, mainactivity. class); // jump to the Weibo homepage startactivity (intent); loginactivity. this. finish ();} else {alertdlgutil. alertdialogneterr (loginactivity. this) ;}}) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {menu. add (menu. first, 0, 0, R. string. user_instead ). seticon (R. drawable. switchuser); menu. add (menu. first, 1, 1, R. string. exit_app ). seticon (R. drawable. menu_exit); Return true ;}@ overridepublic Boolean oncontextitemselected (menuitem item) {Switch (item. getitemid () {Case 0: // switch the account and use the new account to log on to intent = new intent (loginactivity. this, oauthactivity. class); startactivity (intent); break; Case 1: // exit the mainservice program. exitapp (loginactivity. this); break;} return true ;}}

Note 1: Here I only retrieve the first authorized user, and you can retrieve all of them to allow the user to log on.

Through the above code, we can use an authorized user to log on, and use the accessToken method in the Weibo class to request the API for the desired operation!

If you have any questions, please leave a message so that you can learn and communicate with each other!

Note: Please indicate the source for reprinting!

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.