Sunday 6 April 2014

Liferay Mobile SDK+ Android + Custom Services part-3

Hello again,


Please refer Liferay Mobile SDK + Custom Services part-2 for getting idea of what this post is all about.

Now lets see how to use that custom service jar in your native android application.


if you are Android Developer then it will be peace of cake for you because you just have to add that jar in your classpath and direclty make a Asynchronous call of that particular method from the application.


Now lets start step by step,


1) In previous post we have already added that jar in android classpath project.


2) For practise you can download sample android app here.


3) Android doesn't allow synchronous HTTP requests, You can only make them from different threads; for example, from within an AsyncTask instance.

The SDK can help you make asynchronous HTTP requests if you don't want to create an AsyncTask yourself. It makes the service call from an AsyncTask and you can pass a callback that is invoked after the request finishes.
Set a callback class implementation to the session and all the following service calls will be asynchronous. Set it back to null if you want to make synchronous calls again.
This thing you can check in your sample android application.
If we want to call our custom services which we have developed in Part-1 then we have to call that service like below.

 AsyncTaskCallback callback = new IntegerAsyncTaskCallback() {  
   public void onFailure(Exception exception) {  
     // Implement exception handling code  
   }  
   public void onSuccess(int result) {  
     // Called after request has finished successfully  
   }  
 };  
 session.setCallback(callback);  
 customservice.getPotraitId(userId);  


When a ServerException occurs, it's because something went wrong on the server side. For example, if you pass a userId that doesn't exist, the portal will complain about it and the SDK will wrap the error message with a ServerException.
There are many AsyncTaskCallback implementations, one for each service method return type: JSONObjectAsyncTaskCallbackJSONArrayAsyncTaskCallbackStringAsyncTaskCallbackBooleanAsyncTaskCallbackIntegerAsyncTaskCallbackLongAsyncTaskCallback, and DoubleAsyncTaskCallback. Pick the appropriate implementation for your service method return type. In the example above, since getPotraitId returns a Integer, you must use the IntegerAsyncTaskCallback instance.
It's also possible to use a generic AsyncTaskCallback implementation called GenericAsyncTaskCallback. For this implementation, you must implement a transform method and handle JSON parsing by yourself.
Since the request is asynchronous, service.getPotraitId returns immediately with a null object. The result will be passed to the callback onSuccess method instead.
You can also invoke batch commands the SDK allows sending requests using batch processing..You can explore API by yourself for that ;)
Thanks...
GOOD DAY

No comments:

Post a Comment