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-androidtgz 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值