77
What’s New in Liferay Mobile SDK 2.0 for Android Silvio Santos, Mobile Lead Engineer DEVCON | MODCONF 2016

What's new in Liferay Mobile SDK 2.0 for Android

Embed Size (px)

Citation preview

What’s New in Liferay Mobile SDK 2.0 for Android

Silvio Santos, Mobile Lead Engineer

DEVCON | MODCONF 2016

2012

http://localhost:8080/api/jsonws

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

MOBILE SDK

2.0

WHAT’S NEW?

1. @NNOTATION BASED API2. DEFAULT PARAMETERS3. POJO AUTO PARSING4. REQUEST CANCELATION5. BETTER CONFIGURATION API6. RxJAVA INTEGRATION

@NNOTATION BASED API

CREATING API

public interface GroupService { Call<JsonArray> getUserSites(); }

CREATING API

@Path("/group") public interface GroupService { Call<JsonArray> getUserSites(); }

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();}

INSTANTIATE SERVICE

GroupService service = ServiceBuilder.build( GroupService.class);

INVOKE METHOD

Call<JsonArray> call = service.getUserSites();

JsonArray sites = call.execute();

INVOKE METHOD

Call<JsonArray> call = service.getUserSites();

JsonArray sites = call.execute();

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);

POJOAUTO PARSING

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();}

CANCELLING REQUESTS

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)

BETTER REQUEST CONFIGURATION

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

BATCH REQUESTS

Call<User> call1 = service.getUserByEmailAddress( 20116, "[email protected]"); Call<User> call2 = service.getUserByEmailAddress( 20116, "[email protected]"); Response response = Batch.execute(call1, call2);

RxJavaIntegration

RxJava

time

RxJava

1 32

data

RxJava

1 32

completed

RxJava

1 32

error

RxJava

1 32

Observable data stream

RxJava

1 32

Observable data stream

Subscribe

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 OPERATORS

mapfilter

concatzip

cachedebounce

MAP

map(x -> x + 1)

1 32

2 43

FILTER

filter(x -> x > 1)

1 32

32

CONCAT

concat

1 32

54

321 4 5

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 -> { });

JVMCOMPATIBLE

Thanks!

DEVCON | MODCONF 2016

t @sgosantos

[email protected]