Pustaka untuk layanan backend dari nowdb.net
tambahkan pada build.gradle
repositories { // pastikan bukan di dalam buildscript
jcenter()
mavenCentral()
maven {
url 'https://jitpack.io/'
}
}
dependencies {
compile 'com.github.jajanjawa:jnowdb:0.4.1'
// atau
//compile 'com.github.jajanjawa:jnowdb:-SNAPSHOT'
}
NowDBConfig config = new NowDBConfig()
.token(TOKEN)
.project(PROJECT)
.appId(APP_ID);
NowDB nowDb = new NowDB.Builder().config(config).build();
Map<String, Object> data = new HashMap<>();
data.put("nama", "Kucing");
data.put("warna", "Putih");
data.put("timestamp", System.currentTimeMillis());
NowDBResponse response = nowDb.post("hewan", data).execute();
public class Animal extends NowDBCollection {
@SerializedName("nama")
private String name;
@SerializedName("warna")
private String color;
@SerializedName("timestamp")
private Long timestamp;
@Override
public String getCollectionName() {
return "hewan";
}
}
Animal animal = new Animal();
animal.setName("Kucing");
animal.setColor("Putih");
animal.timestamp(System.currentTimeMillis());
NowDBResponse response = nowDb.post(animal).execute();
Animal result = response.as(Animal.class);
String id = result.getId();
class Animal
diatas menggunakan anotasi Gson pada tiap field.
Map<String, Object> data = new HashMap<>();
// isi data kamu
String id = "ID"; // didapat saat melakukan POST
NowDBResponse response = nowDb.put("hewan", id, data).execute();
Status status = response.status();
Animal animal = new Animal();
// setel data kamu
animal.setId(ID); // didapat saat melakukan POST
NowDBResponse response = nowDb.put(animal).execute();
Status status = response.status();
NowDBQuery query = new NowDBQuery("hewan")
.equal("nama", "Kucing")
.and()
.equal("warna", "Putih");
Map<String, Object> data = new HashMap<>();
data.put("nama", "Kelinci"); // diganti kelinci
data.put("warna", "Putih");
data.put("timestamp", System.currentTimeMillis());
NowDBResponse response = nowDb.put(query, data).execute();
Status status = response.status();
String id = "ID";
NowDBResponse response = nowDb.get("hewan", id).execute();
Animal animal = response.as(Animal.class);
Integer limit = 20;
Integer offset = 5;
NowDBResponse response = nowDb.get("hewan", offset, limit).execute();
List<Animal> animals = response.list(Animal.class);
// mencari 5 kelinci
NowDBQuery query = new NowDBQuery("hewan")
.equal("nama", "Kelinci")
.limit(5);
NowDBResponse response = nowDb.get(query).execute();
List<Animal> animals = response.list(Animal.class);
pasti sudah bisa.