A simple startup process for Android-based applications for accessing the server is described here.
You must also deploy an adapter. For more information about using adapters, see Client access to adapters.
This example is based on the sample code created in Setting up Android Studio projects with Gradle.
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.worklight.common.Logger;
import com.worklight.wlclient.api.WLAccessTokenListener;
import com.worklight.wlclient.api.WLAuthorizationManager;
import com.worklight.wlclient.api.WLClient;
import com.worklight.wlclient.api.WLFailResponse;
import com.worklight.wlclient.api.WLResourceRequest;
import com.worklight.wlclient.api.WLResponse;
import com.worklight.wlclient.api.WLResponseListener;
import com.worklight.wlclient.auth.AccessToken;
import com.worklight.wlclient.auth.WLAuthorizationManagerInternal;
import java.net.URI;
import java.net.URISyntaxException;
The WLClient is used throughout the application to connect to the server.
The resource request calls the getBankBalance adapter and writes messages to the log for success or failure.
Replace the entire MainActivity with the code that follows.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WLClient client = WLClient.createInstance(this);
URI adapterPath = null;
try {
adapterPath = new URI("/adapters/javaAdapter/users/getBankBalance");
} catch (URISyntaxException e) {
e.printStackTrace();
}
WLResourceRequest request = new WLResourceRequest(adapterPath, WLResourceRequest.GET);
request.send(new WLResponseListener() {
@Override
public void onSuccess(WLResponse wlResponse) {
Log.i("MobileFirst Quick Start", "Success: " + wlResponse.getResponseText());
}
@Override
public void onFailure(WLFailResponse wlFailResponse) {
Log.i("MobileFirst Quick Start", "Failure: " + wlFailResponse.getErrorMsg());
}
});
}
To test the server connectivity without deploying an adapter, request an access token. If no security checks were added to your app, and the server can be accessed, the token can be obtained. The token request is sent without a scope:
WLAuthorizationManager.getInstance().obtainAccessToken("", new MyObtainAuthorizationHeaderListener());
The entire MainActivity consists of creating the WLClient and requesting the access token:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WLClient client = WLClient.createInstance(this);
URI adapterPath = null;
Logger.setContext(this);
WLAuthorizationManager.getInstance().obtainAccessToken("", new MyObtainAuthorizationHeaderListener());
}
The MyObtainAuthorizationHeaderListener listener returns success or error messages.
class MyObtainAuthorizationHeaderListener implements WLAccessTokenListener {
@Override
public void onSuccess(AccessToken accessToken) {
Log.i("MobileFirst Quick Start", " Success ");
return;
}
@Override
public void onFailure(WLFailResponse wlFailResponse) {
String errorMsg=wlFailResponse.getErrorMsg();
if (errorMsg != null)
errorMsg=errorMsg.replace("\n","").replace("\r","").replace("\t","");
Log.i("MobileFirst Quick Start", "failure: " + errorMsg);
return;
}
}
The error or success message is printed to the Android monitor pane in Android Studio.