掲示板

Invoking services asynchronously in android without an explicit try catch

thumbnail
8年前 に Denis Signoretto によって更新されました。

Invoking services asynchronously in android without an explicit try catch

Expert 投稿: 375 参加年月日: 09/04/21 最新の投稿
Hi,

I'm not an expert of Android programming and I'm writing some code using Mobile SDK for Android (sorry if I'll write something wrong).

In Android when I need to invoke services asynchronously (like documentation suggests at the moment I'm writing)
I had to write my own callback or use an existing one and invoke the service creating first a session and specifing a callback.
When I invoke the service method I must surround it with a try catch block because its signature being the same of sync case and it correcly throws an Exception.

AsyncTaskCallback callback = new JSONArrayAsyncTaskCallback() {

    public void onFailure(Exception exception) {
        // Implement exception handling code
    }

    public void onSuccess(JSONArray result) {
        // Called after request has finished successfully
    }

};

UserService userService = new UserService(session);
session.setCallback(callback);
try {
    service.getGroupEntries(10184, 0, 0, -1, -1);
catch (Exception e) { ... }


In async call the exception is already managed by onFailure() method. Can make sense overload service methods with the possibility to invoke a service passing the callback, returning void and without throwing exception? E.g.
UserService userService = new UserService(session, callback);
service.getGroupEntries(10184, 0, 0, -1, -1);


Is there an already existing way getting shorter code ?
(Am I misleading and there is a good reason to keep an explicit try catch block) ?

Thanks,
Denis.
thumbnail
8年前 に Javier Gamarra によって更新されました。

RE: Invoking services asynchronously in android without an explicit try cat

Expert 投稿: 348 参加年月日: 15/02/12 最新の投稿
The exception is also thrown if something fails when creating the JSON Object to do the request (like a parameter being null when it shouldn't) but yeah, the main reason there is an exception is because the generated method and we can't assume there is a callback present for the sync case.

We'll try to simplify the code, I don't know if presenting two methods with different signatures would confuse the user... I'll discuss it with Bruno Farache and let you know emoticon
thumbnail
8年前 に Bruno Farache によって更新されました。

RE: Invoking services asynchronously in android without an explicit try cat

Liferay Master 投稿: 603 参加年月日: 07/05/14 最新の投稿
Yes, it's because the same service method can be used by both async and sync calls. For async calls, something wrong can happen even before sending the request (like Javier said, if the JSON is malformed, or if session.invoke(_command) throws something too).

If you still have the callback reference you can just call onFailure directly:

catch (Exception e) {
callback.onFailure(e);
}
thumbnail
8年前 に Bruno Farache によって更新されました。

RE: Invoking services asynchronously in android without an explicit try cat

Liferay Master 投稿: 603 参加年月日: 07/05/14 最新の投稿
A good solution would be for the generated methods to return a Call object instead, and the user would decide if that call is async or sync, like this:

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

// Sync

try {
JSONArray sites = call.execute();
}
catch (Exception e) {}

// Async

call.async(new JSONArrayCallback() { ... });

This would break all our APIs though. Maybe it's a good idea to do it now since we are about to release 2.0.
thumbnail
8年前 に Denis Signoretto によって更新されました。

RE: Invoking services asynchronously in android without an explicit try cat

Expert 投稿: 375 参加年月日: 09/04/21 最新の投稿
+1

Great!