Deployd is a tool that makes building APIs simple by providing important ready-made functionality out of the box that meet the demands of complex applications (http://deployd.com). DPD-Android is an android library, that helps facilitate the use of Deployd for Android Development.
#Features
- DPDObject
- DPDUser (Login, Logout)
- DPDQuery
- DPDRequest
- DPDCLient
#The Basics
-
DPD-Android Uses Jackson Library for object mapping. More information can be found here.
-https://github.com/FasterXML/jackson-core -
Assuming we have a collection on Deployd called Stores. We can access the store collection as follows.
-
Using DPDClient
//========================== Inside your application class ====================
public class DPDApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//root url is the deployd application url. replace with your own url
//DPD-Android also support OAuth. if you have accesstoken and refresh token resources
//you can use the following newInstance method
//DPDClient.newInstance(getApplicationContext(), "http://10.0.0.0:2403/", true, "accesstoken", true, "refreshaccesstoken", 433);
DPDClient.newInstance(getApplicationContext(), "http://10.0.0.0:2403/");
}
}- Using DPDObject
public class Store extends DPDObject {
private String name;
private String city;
private String state;
private String zip;
public Store() {
}
public Store(String name, String city, String state, String zip) {
this.name = name;
this.city = city;
this.state = state;
this.zip = zip;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
}
============================ Inside Activity ============================
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createStore();
//loadStore();
}
void createStore() {
Store store = new Store("Best Buy", "Orlando", "Florida", "32818");
store.createObject("store", Store.class, new MappableResponseCallBack() {
@Override
public void onResponse(List<DPDObject> response) {
if (response != null) {
Store store = (Store) response.get(0);
}
}
@Override
public void onFailure(Call call, Response response, Exception e) {
Log.d(this.getClass().getSimpleName(), "Failed to create store");
}
});
}
void loadStore() {
DPDQuery query = new DPDQuery(QueryCondition.EQUAL, null, null, null, "name", "Best Buy", null);
query.findMappableObject("stores", Store.class, new MappableResponseCallBack() {
@Override
public void onResponse(List<DPDObject> response) {
Store store = (Store) response.get(0);
}
@Override
public void onFailure(Call call, Response response, Exception e) {
Log.d(this.getClass().getSimpleName(), "Failed to create store");
}
});
}
}- SubClassing DPDUser
public class User extends DPDUser {
private String firstName;
private String lastName;
private String fullName;
private Integer age;
public User() {
}
public User(String firstName, String lastName, String fullName, Integer age) {
this.firstName = firstName;
this.lastName = lastName;
this.fullName = fullName;
this.age = age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
//========================== Creating a User ===============================
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createUser();
}
void createUser() {
try {
DPDUser.createUser("users", "dpd-android", "dpd-android", User.class, new MappableResponseCallBack() {
@Override
public void onResponse(List<DPDObject> response) {
Log.d(this.getClass().getSimpleName(), "User created successfully");
}
@Override
public void onFailure(Call call, Response response, Exception e) {
Log.d(this.getClass().getSimpleName(), "Failed to create user");
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
void login() {
try {
DPDUser.login("users", "[email protected]", "dpdandroid", User.class, new MappableResponseCallBack() {
@Override
public void onResponse(List<DPDObject> response) {
if (response != null) {
User user = (User)response.get(0);
}
}
@Override
public void onFailure(Call call, Response response, Exception e) {
Log.d(this.getClass().getSimpleName(), "error occured");
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
void updateUser() {
try {
User user = (User) DPDUser.getInstance().currentUser(User.class);
user.setFirstName("John");
user.setLastName("Doe");
user.updateObject("users", User.class, new MappableResponseCallBack() {
@Override
public void onResponse(List<DPDObject> response) {
Log.d(this.getClass().getSimpleName(), "User updated successfully");
}
@Override
public void onFailure(Call call, Response response, Exception e) {
Log.d(this.getClass().getSimpleName(), "Failed to update user");
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}DPD-Android can be added to your project using gradle
dependencies {
compile 'com.ssylveus.dpd-android:dpd-android:0.90.17'
}If you are getting the following error when running your application due to jackson 'Duplicate files copied in APK META-INF/LICENSE' add the followig to gradle inside android method.
android {
packagingOptions {
exclude 'META-INF/LICENSE'
}
}