View
38
Download
0
Embed Size (px)
Citation preview
What’s New in Liferay Mobile SDK 2.0 for Android
Silvio Santos, Mobile Lead Engineer
DEVCON | MODCONF 2016
BUILD REQUEST
JSONObject command = new JSONObject(); String method = "/dlapp/add-folder"; JSONObject params = new JSONObject(); params.put("repositoryId", repositoryId); params.put("parentFolderId", parentFolderId); params.put("name", name); params.put("description", description); command.put(method, params);
DefaultHttpClient client = new DefaultHttpClient(); AuthScope authScope = new AuthScope( AuthScope.ANY_HOST, AuthScope.ANY_PORT); Credentials credentials = new UsernamePasswordCredentials( username, password); client.getCredentialsProvider().setCredentials( authScope, credentials);
CONFIGURE HTTP CLIENT
EXECUTE REQUEST
HttpPost post = new HttpPost(url); post.setEntity(new StringEntity(command.toString())); HttpResponse httpResponse = client.execute(post); HttpEntity entity = httpResponse.getEntity(); String response = EntityUtils.toString(entity);
…handle exceptions
1. @NNOTATION BASED API2. DEFAULT PARAMETERS3. POJO AUTO PARSING4. REQUEST CANCELATION5. BETTER CONFIGURATION API6. RxJAVA INTEGRATION
CREATING API
@Path("/group") public interface GroupService { @Path(“/get-user-sites-groups") Call<JsonArray> getUserSites(); }
CREATING API
@Path("/group") public interface GroupService { @Path(“/get-user-sites-groups") Call<JsonArray> getUserSites();}
ASYNC INVOKE METHOD
call.async(new Callback<JsonArray>() { public void onFailure(Exception exception) { } public void onSuccess(JsonArray result) { }
});
ASYNC INVOKE METHOD
call.async(new Callback<JsonArray>() { public void onFailure(Exception exception) { } public void onSuccess(JsonArray result) { }
});
PARAMETERS
Call<JsonObject> addFolder( long repositoryId, long parentId, String name, String description);
PARAMETERS
@Path("/add-folder") Call<JsonObject> addFolder( long repositoryId, long parentId, String name, String description);
PARAMETERS
@Path("/add-folder") Call<JsonObject> addFolder( @Param(name = "repositoryId") long repositoryId, @Param(name = "parentFolderId") long parentId, @Param(name = "name") String name, @Param(name = “description”) String description);
PARAMETERS
@Path("/add-folder")Call<JsonObject> addFolder( @Param(name = "repositoryId") long repositoryId, @Param(name = "parentFolderId") long parentId, @Param(name = "name") String name, @Param(name = “description”) String description);
PARAMETERS
@Path("/add-folder") Call<JsonObject> addFolder( @Param(name = "repositoryId") long repositoryId, @Param(name = "parentFolderId") long parentId, @Param(name = "name") String name, @Param(name = “description”) String description);
PARAMETERS
@Path("/add-folder") Call<JsonObject> addFolder( @Param(name = "repositoryId") long repositoryId, @Param(name = "parentFolderId") long parentId, @Param(name = "name") String name);
DEFAULT PARAMETERS
@Path("/add-folder") @Params({ @Param(name = "description", value = "") }) Call<JsonObject> addFolder( @Param(name = "repositoryId") long repositoryId, @Param(name = "parentFolderId") long parentId, @Param(name = "name") String name);
DEFAULT PARAMETERS
@Path("/add-folder")@Params({ @Param(name = "description", value = "") }) Call<JsonObject> addFolder( @Param(name = "repositoryId") long repositoryId, @Param(name = “parentFolderId") long parentId, @Param(name = "name") String name);
POJO AUTO PARSING
public class Site { boolean active; long groupId; String name; String descriptiveName; }
POJO AUTO PARSING
@Path("/group") public interface GroupService { @Path(“/get-user-sites-groups") Call<JsonArray> getUserSites(); }
POJO AUTO PARSING
@Path("/group")public interface GroupService { @Path(“/get-user-sites-groups") Call<JsonArray> getUserSites();}
POJO AUTO PARSING
@Path("/group")public interface GroupService { @Path(“/get-user-sites-groups") Call<List<Site>> getUserSites();}
CANCEL REQUESTSCall<List<Site>> call = service.getUserSites();
call.async(new Callback<List<Site>>() {
public void onFailure(Exception exception) { } public void onSuccess(List<Site> sites) { } });
Call.cancel(call)
CANCEL REQUESTSCall<List<Site>> call = service.getUserSites();
call.async(new Callback<List<Site>>() {
public void onFailure(Exception exception) { } public void onSuccess(List<Site> sites) { } });
Call.cancel(call)
CONFIGURING REQUESTS
Config config = new Config.Builder( “http://localhost:8080”) .auth(new BasicAuthentication("login", "pwd")) .header("headerKey", "headerValue") .timeout(10000) .build(); Config.global(config);
CONFIGURING REQUESTS
Config config = new Config.Builder( “http://localhost:8080”) .auth(new BasicAuthentication("login", "pwd")) .header("headerKey", "headerValue") .timeout(10000) .build(); Config.global(config);
CONFIGURING REQUESTS
Config config = new Config.Builder( “http://localhost:8080”) .auth(new BasicAuthentication("login", "pwd")) .header("headerKey", "headerValue") .timeout(10000) .build(); Config.global(config);
CONFIGURING REQUESTS
Config config = new Config.Builder( “http://localhost:8080”) .auth(new BasicAuthentication("login", "pwd")) .header("headerKey", "headerValue") .timeout(10000) .build(); Config.global(config);
CONFIGURING REQUESTS
Config config = Config.global() .newBuilder() .header("key", "value") .build(); Call<JsonArray> call = service.getUserSites();
JsonArray sites = call.execute(config);
CONFIGURING REQUESTS
Config config = Config.global() .newBuilder() .header("key", "value") .build(); Call<JsonArray> call = service.getUserSites();
JsonArray sites = call.execute(config);
BATCH REQUESTS
Call<User> call1 = service.getUserByEmailAddress( 20116, "[email protected]"); Call<User> call2 = service.getUserByEmailAddress( 20116, "[email protected]"); Response response = Batch.execute(call1, call2);
RxJAVA INTEGRATION
@Path("/group") public interface GroupService { @Path(“/get-user-sites-groups") Call<List<Site>> getUserSites(); }
RxJAVA INTEGRATION
@Path("/group")public interface GroupService { @Path(“/get-user-sites-groups") Call<List<Site>> getUserSites();}
RxJAVA INTEGRATION
@Path("/group")public interface GroupService { @Path(“/get-user-sites-groups") Observable<List<Site>> getUserSites();}
RxJAVA INTEGRATION
getUserSites() .subscribe(sites -> { //new data event, do something }, throwable -> { //on error });
RxJAVA INTEGRATION
getUserSites() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(sites -> { }, throwable -> { });
RxJAVA INTEGRATION
getUserSites() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(sites -> { }, throwable -> { });
RxJAVA INTEGRATION
Observable.concat(dbSitesObservable, getUserSites()) .first() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(sites -> { }, throwable -> { });
RxJAVA INTEGRATION
Observable.concat(dbSitesObservable, getUserSites()) .first() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(sites -> { }, throwable -> { });
RxJAVA INTEGRATION
Observable.concat(dbSitesObservable, getUserSites()) .first() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(sites -> { }, throwable -> { });
RxJAVA INTEGRATION
Observable.concat(dbSitesObservable, getUserSites()) .first() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(sites -> { }, throwable -> { });