Skip to content

Client Resources

The ClientResources module allows you to download resources associated with an application to a byte array. This module also provides APIs to download the resource content to a file. SAP Cloud Platform Mobile Services allows administrators to assign various resources to an application.

All the API methods must be invoked from a UI thread. The callback methods are invoked on the UI thread.

Sample Usage

The following sample code shows how to use this API to download the default resource.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
SettingsParameters settingsParameters = new SettingsParameters(hostUrl, APP_ID,  "deviceId", "1.0");
ClientResources clientResources = new ClientResources(okHttpClient, settingsParameters);
clientResources.downloadResource(new ClientResources.CallbackListener<byte[]>() {
        @Override
        public void onSuccess(byte[] byteData){
            //Code to handle the read data
        }

        @Override
        public void onError (Throwable error){
            //Code to handle error
        }
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
val settingsParameters = SettingsParameters(hostUrl, APP_ID, "deviceId", "1.0")
val clientResources = ClientResources(okHttpClient, settingsParameters)
clientResources.downloadResource(object : ClientResources.CallbackListener<ByteArray> {
    override fun onSuccess(byteData: ByteArray) {
        //Code to handle the read data
    }

    override fun onError(error: Throwable) {
         //Code to handle error
    }
 })

The following sample code shows how to use the API to download a specific named resource:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
SettingsParameters settingsParameters = new SettingsParameters(hostUrl, APP_ID,  "deviceId", "1.0");
ClientResources clientResources = new ClientResources(okHttpClient, settingsParameters);
clientResources.downloadResource("resourcename", new ClientResources.CallbackListener<byte[]>() {
    @Override
    public void onSuccess(byte[] byteData) {
        //Code to handle the read data
    }

    @Override
    public void onError(Throwable error) {
        //Code to handle error
    }
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
val settingsParameters = SettingsParameters(hostUrl, APP_ID, "deviceId", "1.0")
val clientResources = ClientResources(okHttpClient, settingsParameters)
clientResources.downloadResource("resourcename", object : ClientResources.CallbackListener<ByteArray> {
    override fun onSuccess(byteData: ByteArray) {
        //Code to handle the read data
    }

    override fun onError(error: Throwable) {
        //Code to handle error
    }
})

The following sample code shows how to use the API to download a specific version of a named resource:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
SettingsParameters settingsParameters = new SettingsParameters(hostUrl, APP_ID,  "deviceId", "1.0");
ClientResources clientResources = new ClientResources(okHttpClient, settingsParameters);
clientResources.downloadResource("resourcename", "1.0", new ClientResources.CallbackListener<byte[]>() {
    @Override
    public void onSuccess( byte[] byteData) {
        //Code to handle the read data
    }

    @Override
    public void onError(Throwable error) {
        //Code to handle error
    }
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
val settingsParameters = SettingsParameters(hostUrl, APP_ID, "deviceId", "1.0")
val clientResources = ClientResources(okHttpClient, settingsParameters)
clientResources.downloadResource("resourcename", "1.0", object : ClientResources.CallbackListener<ByteArray> {
    override fun onSuccess(byteData: ByteArray) {
        //Code to handle the read data
    }

    override fun onError(error: Throwable) {
       //Code to handle error
    }
})

The following sample code shows how to download the default resource and write to a specific file on the device:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
SettingsParameters settingsParameters = new SettingsParameters(hostUrl, APP_ID,  "deviceId", "1.0");
String fileLocation =  Environment.getExternalStorageDirectory().getAbsolutePath();;
ClientResources clientResources = new ClientResources(okHttpClient, settingsParameters);
clientResources.downloadResource(fileLocation + "/filename.ext", true, new ClientResources.CallbackListener<Void>() {
    @Override
    public void onSuccess(Void v) {
        //Handle success callback
    }
    @Override
    public void onError(Throwable error) {
        //Handle the error callback
    }
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
val settingsParameters = SettingsParameters(hostUrl, APP_ID, "deviceId", "1.0")
val fileLocation = Environment.getExternalStorageDirectory().absolutePath
val clientResources = ClientResources(okHttpClient!!, settingsParameters!!)
clientResources.downloadResource("$fileLocation/filename.ext", true, object : ClientResources.CallbackListener<Void> {
    override fun onSuccess(v: Void) {
        //Handle success callback
    }

    override fun onError(error: Throwable) {
        //Handle the error callback
    }
})

The following sample code shows how to download a specific version of a named resource and write to a file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
SettingsParameters settingsParameters = new SettingsParameters(hostUrl, APP_ID,  "deviceId", "1.0");
String fileLocation =  Environment.getExternalStorageDirectory().getAbsolutePath();;
ClientResources clientResources = new ClientResources(okHttpClient, settingsParameters);
clientResources.downloadResource("nameofresource", "1.0", fileLocation + "/filename.ext", true, new ClientResources.CallbackListener<Void>() {
    @Override
    public void onSuccess(Void v) {
        //Handle success callback
    }
    @Override
    public void onError(Throwable error) {
        //Handle the error callback
    }
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
val settingsParameters = SettingsParameters(hostUrl, APP_ID, "deviceId", "1.0")
val fileLocation = Environment.getExternalStorageDirectory().absolutePath
val clientResources = ClientResources(okHttpClient!!, settingsParameters!!)
clientResources.downloadResource("nameofresource", "1.0", "$fileLocation/filename.ext", true, object : ClientResources.CallbackListener<Void> {
    override fun onSuccess(v: Void) {
        //Handle success callback
    }

    override fun onError(error: Throwable) {
        //Handle the error callback
    }
})
The following sample code shows how to get the Resource Information:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
SettingsParameters settingsParameters = new SettingsParameters(hostUrl, APP_ID,  "deviceId", "1.0");
ClientResources clientResources = new ClientResources(okHttpClient, settingsParameters);
clientResources.fetchResourceInfo(new ClientResources.CallbackListener<ResourceBundle[]>() {
    @Override
    public void onSuccess(ResourceBundle[] info) {
        //Handle success callback
        for (int i=0; i< info.length; i++) {
            Log.i("Bundle Version:",info[i].getBundleVersion());
            Log.i("Bundle Extension:", info[i].getBundleExtension());
            Log.i("Bundle Name:", info[i].getBundleName());
            Log.i("Is default:", info[i].isDefault() ? "true" : "false");
        }
    }

    @Override
    public void onError(@NonNull Throwable error) {
        //Handle the error callback
    }
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
val settingsParameters = SettingsParameters(hostUrl, APP_ID, "deviceId", "1.0")
val clientResources = ClientResources(okHttpClient, settingsParameters)
clientResources.fetchResourceInfo(object : ClientResources.CallbackListener<Array<ResourceBundle>> {
    override fun onSuccess(info: Array<ResourceBundle>) {
        //Handle success callback
        for (i in info.indices) {
            Log.i("Bundle Version:", info[i].bundleVersion)
            Log.i("Bundle Extension:", info[i].bundleExtension)
            Log.i("Bundle Name:", info[i].bundleName)
            Log.i("Is default:", if (info[i].isDefault) "true" else "false")
        }
    }

    override fun onError(error: Throwable) {
        //Handle the error callback
    }
})