3. 开发Android应用访问renren.db中的数据
从上面分析中我们已经知道renren.db中表结构,及访问特定表对应的Uri,如我们可以通过content://com.renren.mobile.provider/account访问renren.db中的account表等等。下面我们写个很简单的例子来访问account表中的account和ticket字段。
main.xml根节点下简单添加2个TextView,如下:
复制代码
将account和ticket信息显示到TextView中,类代码如下
复制代码
需要指出的是,上面的应用程序需要操作人人网android客户端中的数据库,因此要记得在AndroidMantifest.xml文件中为该应用程序授权。也就是在该文件的根元素中添加如下元素:
从上面分析中我们已经知道renren.db中表结构,及访问特定表对应的Uri,如我们可以通过content://com.renren.mobile.provider/account访问renren.db中的account表等等。下面我们写个很简单的例子来访问account表中的account和ticket字段。
main.xml根节点下简单添加2个TextView,如下:
- <TextView
- android:id="@+id/textView1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="TextView" />
-
-
- <TextView
- android:id="@+id/textView2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="TextView" />
将account和ticket信息显示到TextView中,类代码如下
- public class RenRenExtActivity extends Activity {
-
- private static final Uri ACCOUNT_CONTENT_URI = Uri
- .parse("content://com.renren.mobile.provider/account");
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- TextView textView1 = (TextView) findViewById(R.id.textView1);
- textView1.setText("Sorry.");
- TextView textView2 = (TextView) findViewById(R.id.textView2);
- textView2.setText("Sorry2.");
-
- Cursor cursor = getContentResolver().query(ACCOUNT_CONTENT_URI, null,
- null, null, null);
-
- List<Map<String, String>> resultList = converCursorToList(cursor);
- if (!resultList.isEmpty()) {
- Map<String, String> map = resultList.get(0);
- textView1.setText(map.get("account"));
- textView2.setText(map.get("ticket"));
- }
- }
-
- private List<Map<String, String>> converCursorToList(Cursor cursor) {
- List<Map<String, String>> result = new ArrayList<Map<String, String>>();
-
- if (cursor == null) {
- return Collections.emptyList();
- }
-
- // 遍历Cursor结果集
- while (cursor.moveToNext()) {
- // 将结果集中的数据存入ArrayList中
- Map<String, String> map = new HashMap<String, String>();
- map.put("account",
- cursor.getString(cursor.getColumnIndex("account")));
- map.put("ticket", cursor.getString(cursor.getColumnIndex("ticket")));
- result.add(map);
- }
- return result;
- }
- }
- <uses-permission android:name="com.renren.mobile.android.permission.PERMISSION_ADD_ACCOUNT" />
如果你android手机中安装有人人网Android客户端且曾经使用过,那么renren.db中应该有数据存在,把上面应用打包为apk文件安装到你android手机中,运行它,应该能看到屏幕中将显示你的人人网账号及一串ticket,该ticket是人人网Andriod客户端部分功能与人人网服务器通信的sid。
同理,也可以使用其它特定Uri访问手机中renre.db中特定的表,如friends表等等,所有Uri详见RenRenProvider代码。