Skip to content

Android 0.6.x

Preethum edited this page Nov 22, 2015 · 4 revisions

Android Library - 0.6.X

The SyncProxy Android library (SPALibrary) version of the gwt-syncproxy project was created to give the same gwt-syncproxy functionality to Android devices, with the specific case of being able to utilize Android's internal account manager system for communicating to Google App Engine services. Additionally, another tool is provided to help manage the network calling to RPC's away from the main thread, ServiceAsyncTask. This task is specifically designed to help make RPC calls and receive responses on the correct threads. Starting in this version 0.6.x, this library is available as an aar with defined dependencies (will be available on maven/jcenter when deployed). See the Repository-Access Wiki.

EnumMap Not Supported on Android

The EnumMap object is not supported on Android because the Serializer provided with GWT serializes a different version of the EnumMap class than is provided in the Android OS.

Service Async Task

The ServiceAsyncTask class provides an easy way to make RPC calls from Android that accommodate for the need to perform network actions in the background while performing UI updates on the main thread. You must also provide a context and a string resource containing a fullformed url for the location of the gwt service files (ex https://www.myapp.com/mymodule or http://192.168.1.100:8888/mymodule). In essence, this class will take care of setting the base URL for SyncProxy, creating the service, making the call, and returning the results to your AsyncCallback. The below example shows you the most common case scenario. The primary action you must do is override the #serviceCall() method and make the call as shown, inserting the appropriate parameters for your RPC.

This class takes as a parameter the AsyncCallback that is typically supplied to the RPC call itself. The purpose behind this is that UI actions done in the callback need to be completed on the main thread. If you call the RPC AsyncCallback from a different thread, then you'll get an error. This class ensures that the AsyncCallback's are called on the main thread. You will override the #serviceCall method in a specific way in order to call the RPC method with your expected parameters. It also takes care of creating the service for you with standard expectations. If you need to customize it, you may need to override other methods in this class (see Javadoc API). Specifically, you'll access your service, which is created automatically within the AsyncTask, using the method #getAsyncService(). You'll call your target method with the parameters you wish, and lastly you'll provide the method with the AsyncCallback provided by the method #getCallback(). The AsyncCallback provided by #getCallback is a small delegator which returns the results of the RPC call (success or failure) back to your AsyncCallback on the main thread (from #onPostExecute()).

ServiceAsyncTask<ServiceAsync, ReturnType> serviceTask = new ServiceAsyncTask<ServiceAsync, ReturnType>(
	Service.class, context, rpcBaseResource, null, new AsyncCallback<ReturnType>() {
	@Override
	public void onSuccess(ReturnType arg0) {
	    // TODO Work with the RPC result
	}
    @Override
	public void onFailure(Throwable arg0) {
	    // TODO Deal with the RPC Failure
	}
}, null) {

	@Override
	public void serviceCall() {
                // getAsyncService accesses the designated service created automatically within the Task
                // getCallback provides a delegating AsyncCallback which will call your specified AsyncCallback from the main thread
		getAsyncService().rpcTargetMethod(/**Additional parameters here**/getCallback());
	}
};
serviceTask.execute(); // Don't Forget to execute the task to make the service call!

As an added feature, you can override the #onProgressUpdate() method to receive and act on the progress through the entire RPC interaction.

	@Override
	protected void onProgressUpdate(ServiceTaskProgress... values) {
		super.onProgressUpdate(values);
		// TODO Act on the values[0]
	}

The values[0] parameter will contain values defined in the ServiceTaskProgress enum. See the Javadoc for details.

Authenticator Options

As of version 0.6, GSP Android library provides a new Authenticator framework to utilize when attempting to work against secured servlets. See the GSP-Authenticator-Framework wiki for details. There are 2 default Android compatible authenticators to choose from.

If you utilize the previously mentioned ServiceAsyncTask, you simply pass this authenticator into the task constructor and GSP will automatically utilize it.

ServiceAsyncTask<ServiceAsync, ReturnType> serviceTask = new CustomServiceAsyncTask<ServiceAsync, ReturnType>(
	Service.class, context, rpcBaseResource, authenticator, new MyAsyncCallback(), null) ;