Saving authorization information for Sina Weibo Development

Source: Internet
Author: User
In order to facilitate your next use, we need to save the authorization information. Here we use SQLite in the android system to save it! For more information about how to use SQLite, see my article: How to Use SQLite in android download management. For the smoothness of the program, we use the service background to save the user authorization information:
User user = (User) task. getTaskParam (). get ("user"); UserInfo userInfo = new UserInfo (); userInfo. setId ("" + user. getId (); // obtain the user IDuserInfo. setToken (OAuthConstant. getInstance (). getToken (); // get tokenuserInfo. setSecret (OAuthConstant. getInstance (). getTokenSecret (); // get TokenSecretuserInfo. setName (user. getName (); // username userInfo. setDrawable (getBitmap (user. getProfileImageUrl (); // user profile url, which is used to log on with DataHelper dataHelper = new DataHelper (MainService. this); dataHelper. saveUserInfo (userInfo); // save user information

The DataHelper class code is as follows:

Package COM. cloay. weibo. DB; import Java. io. bytearrayinputstream; import Java. io. bytearrayoutputstream; import Java. util. arraylist; import Java. util. list; import COM. cloay. weibo. bean. userinfo; import android. content. context; import android. database. cursor; import android. database. SQLite. sqlitedatabase; import android. graphics. bitmap; import android. graphics. bitmapfactory;/*** used to save user authorization information * @ author cloay * 2 012-2-29*08:47:08 */public class datahelper {/*** database name */Private Static string db_name = "cloay. DB ";/*** database version number */Private Static int db_version = 1; private sqlitehelper dbhelper; Public datahelper (context) {dbhelper = new sqlitehelper (context, db_name, null, db_version);}/*** shut down the database */Public void closedb () {dbhelper. close ();}/*** get the userid, access token, access sec in the user_info table RET records * @ Param isall * @ return */public list <userinfo> getuserlist (Boolean isall) {// If isall is true, all messages are obtained. Otherwise, IDs, tokens, secret list <userinfo> userlist = new arraylist <userinfo> (); sqlitedatabase database = dbhelper. getwritabledatabase (); string SQL = "select user_id, Token, secret, name, drawable from user_info"; cursor = database. rawquery (SQL, new string [] {}); While (cursor. movetonext () {useri Nfo info = new userinfo (); info. setid (cursor. getstring (0); info. settoken (cursor. getstring (1); info. setsecret (cursor. getstring (2); If (isall) {info. setname (cursor. getstring (3); bytearrayinputstream stream = new bytearrayinputstream (cursor. getblob (4); bitmap icon = bitmapfactory. decodestream (Stream); info. setdrawable (icon);} userlist. add (Info);} cursor. close (); closedb (); Return userlist ;}/* ** Record for determining whether the users table contains a userid * @ Param userid * @ return */Public Boolean haveuserinfo (string userid) {Boolean ishave = false; sqlitedatabase database = dbhelper. getreadabledatabase (); string SQL = "select count (*) from user_info where user_id =? "; Cursor = database. rawquery (SQL, new string [] {userid}); ishave = cursor. movetofirst (); cursor. close (); closedb (); Return ishave;}/*** add users table records * @ Param user */Public void saveuserinfo (userinfo user) {sqlitedatabase database = dbhelper. getwritabledatabase (); string SQL = "insert into user_info (ID, user_id, Token, secret, name, drawable) values (?,?,?,?,?,?) "; Bytearrayoutputstream OS = new bytearrayoutputstream (); // compress bitmap to PNG encoding and stores the user with a quality of 100%. getdrawable (). compress (bitmap. compressformat. PNG, 100, OS); // id I set to 1 by default. Because I only use one account, you can set it to another object [] bindargs = {"1 ", user. GETID (), user. gettoken (), user. getsecret (), user. getname (), OS. tobytearray ()}; database.exe csql (SQL, bindargs); closedb ();}/*** delete users table record * @ Param userid */Public void deluse Rinfo (string userid) {sqlitedatabase database = dbhelper. getreadabledatabase (); database. Delete ("user_info", "user_id =? ", New string [] {userid}); closedb ();}}

Now, all the authorization and authorization information is complete! The next step is to set up the homepage and get Weibo! Please leave a message if you have any questions!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.