• Save
CouchDB on Android
Upcoming SlideShare
Loading in...5
×
 

CouchDB on Android

on

  • 5,705 views

 

Statistics

Views

Total Views
5,705
Views on SlideShare
5,694
Embed Views
11

Actions

Likes
8
Downloads
0
Comments
0

5 Embeds 11

https://twitter.com 4
http://paper.li 3
http://www.linkedin.com 2
https://si0.twimg.com 1
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

CC Attribution-NonCommercial LicenseCC Attribution-NonCommercial License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

CouchDB on Android CouchDB on Android Presentation Transcript

  • CouchDB on Android
  • aboutSven Haiges, hybris GmbH Twitter @hansamann Android, HTML5, Groovy & Grails sven.haiges@hybris.de
  • aboutSven Haiges, hybris GmbH Twitter @hansamann Android, HTML5, Groovy & Grails sven.haiges@hybris.de
  • CouchDB
  • Erlang on Android!Source: http://couchdb.apache.org/
  • sync
  • CouchDB• document-based, RESTful HTTP API• replication built-in• both database and web server• no locks, uses multi-version concurrency control• Map & Reduce Functions = Views
  • <host>/_utils = Futon Admin
  • Mobile Futon
  • Accessingyour couch
  • adb forward tcp:5999 tcp:33595
  • build your own
  • #1 new android project
  • #2 add couchbase.ziphttp://www.couchbase.org/get/couchbase-mobile-for- android/current
  • #3 run as Ant file
  • done...
  • AndroidManifest.xml<?xml version="1.0" encoding="UTF-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="de.flavor.relax" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".RelaxActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:exported="false" android:enabled="true" android:name="com.couchbase.android.CouchbaseService" /> </application> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /></manifest>
  • in your Activity...private final ICouchbaseDelegate mDelegate = new ICouchbaseDelegate() { public void couchbaseStarted(String host, int port) { Toast.makeText(RelaxActivity.this, host + ":" + port, Toast.LENGTH_LONG).show(); } public void installing(int completed, int total) { Log.d("demo", "Installing... " + completed + "/" + total); } public void exit(String error) { Log.d("demo", error); }};
  • in your Activity...@Overrideprotected void onResume() { super.onResume(); CouchbaseMobile couch = new CouchbaseMobile(getBaseContext(), mDelegate); couchServiceConnection = couch.startCouchbase();}
  • ektorp
  • AndCouch
  • AndCouch• https://github.com/daleharvey/Android- MobileFuton• Just a quick HTTP wrapper, but gets the job done!• CouchDemo App...
  • Todo.javapublic class Todo { private String id; private String rev; private String text; private long timestamp; public Todo(String id, String rev, String text, long timestamp) { super(); this.id = id; this.rev = rev; this.text = text; this.timestamp = timestamp; } ...}
  • CreatingString value = input.getText().toString();JSONObject obj = new JSONObject();try { obj.put("type", "todo"); obj.put("text", value); obj.put("timestamp", new Date().getTime()); JSONObject result = AndCouch.put(baseURL + DB_NAME + "/" + UUID.randomUUID().toString(), obj.toString()).json; if (result.getBoolean("ok") == true) refreshTodos();} catch (JSONException e) { e.printStackTrace();}
  • Reading (all)try { todos.clear(); JSONObject json = AndCouch.get(baseURL + DB_NAME + "/_design/todo/_view/all").json; JSONArray rows = json.getJSONArray("rows"); for (int i = 0; i < rows.length(); i++) { JSONObject todoObj = rows.getJSONObject(i); JSONArray value = todoObj.getJSONArray("value"); todos.add(new Todo(todoObj.getString("id"), value.getString(1), value.getString(0), todoObj.getLong("key"))); } todos.notifyDataSetChanged();} catch (JSONException e) { e.printStackTrace();}
  • Reading (all)try { todos.clear(); JSONObject json = AndCouch.get(baseURL + DB_NAME + "/_design/todo/_view/all").json; JSONArray rows = json.getJSONArray("rows"); for (int i = 0; i < rows.length(); i++) { JSONObject todoObj = rows.getJSONObject(i); JSONArray value = todoObj.getJSONArray("value"); todos.add(new Todo(todoObj.getString("id"), value.getString(1), value.getString(0), todoObj.getLong("key"))); } todos.notifyDataSetChanged();} catch (JSONException e) { e.printStackTrace();}
  • Design Documents• normal json documents, but ids begin with _design/, e.g. _design/myapp (no %2F :-)• contain views (Map & Reduce functions) as well as other application functions (validation, show/list)
  • Simple Design Doc{ "_id":"_design/todo", "views" : { "all" : { "map": "function(doc) { if (doc.type && doc.type === todo){ emit(doc.timestamp, [doc.text, doc._rev]); }};" } }}
  • DeletingSparseBooleanArray spar = getListView().getCheckedItemPositions();for (int i = 0; i < todos.getCount(); i++) { if (spar.get(i)) { Todo todo = todos.getItem(i); getListView().setItemChecked(i, false); try { AndCouch.httpRequest("DELETE", baseURL + DB_NAME + "/" + todo.getId() + "?rev=" + todo.getRev(), null, new String[][] {}); } catch (JSONException e) { e.printStackTrace(); } }}refreshTodos();
  • Remote PushJSONObject obj = new JSONObject();try { obj.put("create_target", true); obj.put("source", baseURL + DB_NAME); obj.put("target", "http:// hansamann.iriscouch.com/todo"); JSONObject result = AndCouch.post(baseURL + "_replicate", obj.toString()).json;} catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace();}
  • CouchApps
  • CouchApps• JavaScript and HTML5 apps served from CouchDB• No middle tier, Couch is DB + application server• CouchApp.org hosts the CouchApp development tool that pushes changes to a Couch instance
  • aboutSven Haiges, hybris GmbH Twitter @hansamann Android, HTML5, Groovy & Grails sven.haiges@hybris.de
  • aboutSven Haiges, hybris GmbH Twitter @hansamann Android, HTML5, Groovy & Grails sven.haiges@hybris.de NFC