Your SlideShare is downloading. ×
  • Like
  • Save
CouchDB on Android
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

CouchDB on Android

  • 5,445 views
Published

 

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
5,445
On SlideShare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
0
Comments
0
Likes
9

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    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

Transcript

  • 1. CouchDB on Android
  • 2. aboutSven Haiges, hybris GmbH Twitter @hansamann Android, HTML5, Groovy & Grails sven.haiges@hybris.de
  • 3. aboutSven Haiges, hybris GmbH Twitter @hansamann Android, HTML5, Groovy & Grails sven.haiges@hybris.de
  • 4. CouchDB
  • 5. Erlang on Android!Source: http://couchdb.apache.org/
  • 6. sync
  • 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. <host>/_utils = Futon Admin
  • 9. Mobile Futon
  • 10. Accessingyour couch
  • 11. adb forward tcp:5999 tcp:33595
  • 12. build your own
  • 13. #1 new android project
  • 14. #2 add couchbase.ziphttp://www.couchbase.org/get/couchbase-mobile-for- android/current
  • 15. #3 run as Ant file
  • 16. done...
  • 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. 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. in your Activity...@Overrideprotected void onResume() { super.onResume(); CouchbaseMobile couch = new CouchbaseMobile(getBaseContext(), mDelegate); couchServiceConnection = couch.startCouchbase();}
  • 20. ektorp
  • 21. AndCouch
  • 22. AndCouch• https://github.com/daleharvey/Android- MobileFuton• Just a quick HTTP wrapper, but gets the job done!• CouchDemo App...
  • 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. 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. 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. 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. 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. 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. 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. 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. CouchApps
  • 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. aboutSven Haiges, hybris GmbH Twitter @hansamann Android, HTML5, Groovy & Grails sven.haiges@hybris.de
  • 34. aboutSven Haiges, hybris GmbH Twitter @hansamann Android, HTML5, Groovy & Grails sven.haiges@hybris.de NFC