CouchDB on Android 入门例子

CouchDB on mobile 是一个将CouchDB应用在移动终端上的项目,他包括一个CouchDB数据库,一个Javascript解析中间层和一个极简地轻量级Web Server,利用这些工具,即可在移动设备上构建本地和远程云存储端相同步地一个数据存储服务,然后利用此服务构建你的移动 App。下面是一个在Android上利用CouchDB构建的一个最简单的例子。

这个App被取名为 native Android App ,你可以直接点击并在你的Android手机上安装体验。对CouchApp还不熟悉的同学可以猛击这个链接 couchapp.org. 在github上你也可以获得 the couchapp 的源码和相关的 android project 的代码。

Warning CouchDB on Android 目前还在beta版本,API的格式可能在以后还会有所更改,所以请最好在别处备份你的重要数据。

好了,下面我们跟着下面的步骤,开始先进行一系列地安装吧

  1. Install the Android SDK
  2. Install CouchApp
  3. Download libcouch-android, tgz or git
  4. Install CouchDB on your device / emulator

安装完毕,开始开发

首先,你最好打开一个log查看器以查看你CouchDB的情况以及浏览器的情况

$ adb logcat

如果你的设备上已经运行有CouchDB了,当出现“CouchDB Running”的提示后,运行下面的命令:

$ adb forward tcp:5985 tcp:5984

这样你就可以通过下面的地址访问到你的CouchDB了 127.0.0.1:5985/_utils

然后在elipse里打开你的 libcouch-android 项目 (步骤:File > New Project > Android Project > Open from existing directory 然后选择你下载的libcouch-android 文件夹)

然后创建一个新的项目, File > New Project > Android Project

打开 AndroidManifest.xml 文件,输入如下内容:

然后确认你的app能够与CouchDB的REST API进行沟通(例子)

将 libcouch-android 添加为你应用的一个类包, (Properties > Android 并选择 libcouch-android)

将 ExampleCouchApp.java 的内容复制到你项目的 /src/com/myname/myapp/MyApp.java 文件中。(别忘了保存你的代码包和类名) 然后再按下面链接对 bootStrapDatabases 和 appToLaunch 进行设置, 设置例子

此json文件 拖到 assets 目录, 这个json文件也可以使用下面命令创建

$ couchapp push --export > filename.json

你的App基本上就完成了,点击eclipse上面的绿色的Run按钮,你的应用就会安装到你的电话里,如果中间有什么问题,查看一下日志 $ adb logcat, 下面就是这个项目的结构:

下面是一些FAQ,就暂时不翻译了,大家有问题可以自行查看一下

FAQ – Developing on android

How do I access CouchDB on my computer

$ adb forward tcp:5985 tcp:5984

Will let you access CouchDB via http://127.0.0.1:5985/_utils

How do I view the CouchDB / Console logs

$ adb logcat

Something broke?

That isnt a question, but please email me at dale@couchbase.com with as much information as possible. The output from $ adb logcat is extremely useful.

How do I build CouchDB for Android myself?

You dont need to build CouchDB yourself to program against it, but if you really want to then there are instructions on the CouchDB wiki

Much thanks to Matt Adams.

How do I use CouchApp to push to my phone?

The same way you push to any other CouchDB, you need to have set up forwaring first, my .couchapprc file looks like:

{
  "env" : {
    "default" : {
      "db" : "http://myname:mypass@127.0.0.1:5984/couchnotes"
    },
    "android" : {
      "db" : "http://com_arandomurl_couchnotes:pass@127.0.0.1:5985/couchnotes-org_android_couchnotes"
    }
  }
}

I tried to create a database with name X and it doesnt exist?

You cant directly create databases as they may conflict with other applications, your databases will be created with your packagename appended (with periods replaced by underscores) so the database created for CouchNotes is names couchnotes-com_arandomurl_couchnotes

How do I find out the credentials to CouchDB databases?

When you create a database you provide a password, a user is created with the username being the package name of your app with periods replaced with underscores (for example com_arandomurl_couchnotes) and they are set as admin on any databases you create.

CouchUtils.java provides a readOrGeneratePass function to help generate and store a password, you can find the generated password by running:

adb shell cat /data/data/com.myname.myapp/files/com_myname_myapp.passwd

Click on the menu button while futon is open and select Admin Password

Is my data really secure?

No, although the SDK provides seperation between applications, currently Android CouchDB stores all data on the SD Card of your phone due to the size of the data. Other applications can access your data directly though the SD Card.

I want a native app, not a CouchApp

The process for building a native application that uses CouchDB is exactly the same, just include libcouch-android as a library to your project, ICouchService.aidl are the functions you use to control CouchDB

How big is CouchDB? / CouchDB is too big for my app

Currently CouchDB is around 17MB uncompressed (about a 10MB download), There is a lot of work being done to reduce the size for both downloading and runtime memory footprint.

How do I use my own CouchApp

$ couchapp push --export > filename.json

Will produce a json file that can be loaded into Couch, the ExampleCouchApp.java will look for a json file in the assets folder for every database that is bootstrapped.

How do I access Geolocation etc from my CouchApp?

PhoneGap is a library that allows you to access native API’s via Javascript and is an excellent compliment to CouchDB on Android.

How do I publish my App?

Just follow the standard Android publishing procedure

CouchDB Reloads when I change orientation

Android by default will destroy your activity when your phone changes orientation, you can use

android:configChanges="keyboardHidden|orientation"

to avoid that, here is a working example.

原文链接:http://goo.gl/Sh6TN

anyShare据说看到好文章不转的人,服务器容易宕机!
          

无觅相关文章插件,快速提升流量