CouchDB on Android

7,325 views

Published on

Published in: Technology
1 Comment
13 Likes
Statistics
Notes
  • I use project of : https://github.com/daleharvey/Android- MobileFuton. and try to "creat" and "read", "creat" is ok but "read" always return an empty JSON. Please check your project for this. Thanks!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
7,325
On SlideShare
0
From Embeds
0
Number of Embeds
22
Actions
Shares
0
Downloads
0
Comments
1
Likes
13
Embeds 0
No embeds

No notes for slide
  • \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

    1. 1. CouchDB on Android
    2. 2. aboutSven Haiges, hybris GmbH Twitter @hansamann Android, HTML5, Groovy & Grails sven.haiges@hybris.de
    3. 3. aboutSven Haiges, hybris GmbH Twitter @hansamann Android, HTML5, Groovy & Grails sven.haiges@hybris.de
    4. 4. CouchDB
    5. 5. Erlang on Android!Source: http://couchdb.apache.org/
    6. 6. sync
    7. 7. 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
    8. 8. <host>/_utils = Futon Admin
    9. 9. Mobile Futon
    10. 10. Accessingyour couch
    11. 11. adb forward tcp:5999 tcp:33595
    12. 12. build your own
    13. 13. #1 new android project
    14. 14. #2 add couchbase.ziphttp://www.couchbase.org/get/couchbase-mobile-for- android/current
    15. 15. #3 run as Ant file
    16. 16. done...
    17. 17. 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>
    18. 18. 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); }};
    19. 19. in your Activity...@Overrideprotected void onResume() { super.onResume(); CouchbaseMobile couch = new CouchbaseMobile(getBaseContext(), mDelegate); couchServiceConnection = couch.startCouchbase();}
    20. 20. ektorp
    21. 21. AndCouch
    22. 22. AndCouch• https://github.com/daleharvey/Android- MobileFuton• Just a quick HTTP wrapper, but gets the job done!• CouchDemo App...
    23. 23. 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; } ...}
    24. 24. 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();}
    25. 25. 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();}
    26. 26. 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();}
    27. 27. 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)
    28. 28. Simple Design Doc{ "_id":"_design/todo", "views" : { "all" : { "map": "function(doc) { if (doc.type && doc.type === todo){ emit(doc.timestamp, [doc.text, doc._rev]); }};" } }}
    29. 29. 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();
    30. 30. 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();}
    31. 31. CouchApps
    32. 32. 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
    33. 33. aboutSven Haiges, hybris GmbH Twitter @hansamann Android, HTML5, Groovy & Grails sven.haiges@hybris.de
    34. 34. aboutSven Haiges, hybris GmbH Twitter @hansamann Android, HTML5, Groovy & Grails sven.haiges@hybris.de NFC

    ×