User user = (User) task.getTaskParam().get("user"); UserInfo userInfo = new UserInfo(); userInfo.setId(""+user.getId()); //获取用户ID userInfo.setToken(OAuthConstant.getInstance().getToken()); //获取token userInfo.setSecret(OAuthConstant.getInstance().getTokenSecret()); //获取TokenSecret userInfo.setName(user.getName()); //用户名 userInfo.setDrawable(getBitmap(user.getProfileImageUrl())); //用户头像URL地址,用来登录时使用 DataHelper dataHelper = new DataHelper(MainService.this); dataHelper.SaveUserInfo(userInfo); //保存用户信息其中DataHelper类代码如下:
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; /** * 用来保存用户授权相关信息 * @author Cloay * 2012-2-29 * 下午08:47:08 */ public class DataHelper { /** * 数据库名称 */ private static String DB_NAME = "cloay.db"; /** * 数据库版本号 */ private static int DB_VERSION = 1; private sqliteHelper dbHelper; public DataHelper(Context context){ dbHelper=new sqliteHelper(context,DB_NAME,null,DB_VERSION); } /** * 关闭数据库 */ public void closeDB() { dbHelper.close(); } /** * 获取user_info表中的UserID、Access Token、Access Secret的记录 * @param isAll * @return */ public List<UserInfo> GetUserList(Boolean isAll){//isAll为true时获取全部消息,否则获取id,token,secret List<UserInfo> userList = new ArrayList<UserInfo>(); sqliteDatabase database = dbHelper.getWritableDatabase(); String sql = "select user_id,secret,name,drawable from user_info"; Cursor cursor = database.rawQuery(sql,new String[] {}); while (cursor.moveToNext()) { UserInfo 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; } /** * 判断users表中的是否包含某个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 cursor = database.rawQuery(sql,new String[] { UserId }); isHave = cursor.moveToFirst(); cursor.close(); closeDB(); return isHave; } /** * 添加users表的记录 * @param user */ public void SaveUserInfo(UserInfo user){ sqliteDatabase database = dbHelper.getWritableDatabase(); String sql = "insert into user_info(id,user_id,drawable) values (?,?,?)"; ByteArrayOutputStream os = new ByteArrayOutputStream(); // 将Bitmap压缩成PNG编码,质量为100%存储 user.getDrawable().compress(Bitmap.CompressFormat.PNG,100,os); //id我默认都为1了因为我只用了一个账号,大家保存的时候可设置成其他的 Object[] bindArgs = {"1",user.getId(),user.getToken(),user.getSecret(),user.getName(),os.toByteArray()}; database.execsql(sql,bindArgs); closeDB(); } /** * 删除users表的记录 * @param UserId */ public void DelUserInfo(String UserId){ sqliteDatabase database = dbHelper.getReadableDatabase(); database.delete("user_info","user_id=?",new String[] { UserId }); closeDB(); } }
好了关于授权及授权信息的内容到此就全部完成了!接下来就是主页的搭建及微博的获取了!
有问题请留言,大家一起交流学习!
说明:转载请注明出处!
原文链接:https://www.f2er.com/sqlite/202130.html