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.
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
SettingsParameterssettingsParameters=newSettingsParameters(hostUrl,APP_ID,"deviceId","1.0");ClientResourcesclientResources=newClientResources(okHttpClient,settingsParameters);clientResources.downloadResource(newClientResources.CallbackListener<byte[]>(){@OverridepublicvoidonSuccess(byte[]byteData){//Code to handle the read data}@OverridepublicvoidonError(Throwableerror){//Code to handle error}});
1
2
3
4
5
6
7
8
9
10
11
valsettingsParameters=SettingsParameters(hostUrl,APP_ID,"deviceId","1.0")valclientResources=ClientResources(okHttpClient,settingsParameters)clientResources.downloadResource(object: ClientResources.CallbackListener<ByteArray>{overridefunonSuccess(byteData:ByteArray){//Code to handle the read data}overridefunonError(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
SettingsParameterssettingsParameters=newSettingsParameters(hostUrl,APP_ID,"deviceId","1.0");ClientResourcesclientResources=newClientResources(okHttpClient,settingsParameters);clientResources.downloadResource("resourcename",newClientResources.CallbackListener<byte[]>(){@OverridepublicvoidonSuccess(byte[]byteData){//Code to handle the read data}@OverridepublicvoidonError(Throwableerror){//Code to handle error}});
1
2
3
4
5
6
7
8
9
10
11
valsettingsParameters=SettingsParameters(hostUrl,APP_ID,"deviceId","1.0")valclientResources=ClientResources(okHttpClient,settingsParameters)clientResources.downloadResource("resourcename",object: ClientResources.CallbackListener<ByteArray>{overridefunonSuccess(byteData:ByteArray){//Code to handle the read data}overridefunonError(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
SettingsParameterssettingsParameters=newSettingsParameters(hostUrl,APP_ID,"deviceId","1.0");ClientResourcesclientResources=newClientResources(okHttpClient,settingsParameters);clientResources.downloadResource("resourcename","1.0",newClientResources.CallbackListener<byte[]>(){@OverridepublicvoidonSuccess(byte[]byteData){//Code to handle the read data}@OverridepublicvoidonError(Throwableerror){//Code to handle error}});
1
2
3
4
5
6
7
8
9
10
11
valsettingsParameters=SettingsParameters(hostUrl,APP_ID,"deviceId","1.0")valclientResources=ClientResources(okHttpClient,settingsParameters)clientResources.downloadResource("resourcename","1.0",object: ClientResources.CallbackListener<ByteArray>{overridefunonSuccess(byteData:ByteArray){//Code to handle the read data}overridefunonError(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
SettingsParameterssettingsParameters=newSettingsParameters(hostUrl,APP_ID,"deviceId","1.0");StringfileLocation=Environment.getExternalStorageDirectory().getAbsolutePath();;ClientResourcesclientResources=newClientResources(okHttpClient,settingsParameters);clientResources.downloadResource(fileLocation+"/filename.ext",true,newClientResources.CallbackListener<Void>(){@OverridepublicvoidonSuccess(Voidv){//Handle success callback}@OverridepublicvoidonError(Throwableerror){//Handle the error callback}});
1
2
3
4
5
6
7
8
9
10
11
12
valsettingsParameters=SettingsParameters(hostUrl,APP_ID,"deviceId","1.0")valfileLocation=Environment.getExternalStorageDirectory().absolutePathvalclientResources=ClientResources(okHttpClient!!,settingsParameters!!)clientResources.downloadResource("$fileLocation/filename.ext",true,object: ClientResources.CallbackListener<Void>{overridefunonSuccess(v:Void){//Handle success callback}overridefunonError(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
SettingsParameterssettingsParameters=newSettingsParameters(hostUrl,APP_ID,"deviceId","1.0");StringfileLocation=Environment.getExternalStorageDirectory().getAbsolutePath();;ClientResourcesclientResources=newClientResources(okHttpClient,settingsParameters);clientResources.downloadResource("nameofresource","1.0",fileLocation+"/filename.ext",true,newClientResources.CallbackListener<Void>(){@OverridepublicvoidonSuccess(Voidv){//Handle success callback}@OverridepublicvoidonError(Throwableerror){//Handle the error callback}});
1
2
3
4
5
6
7
8
9
10
11
12
valsettingsParameters=SettingsParameters(hostUrl,APP_ID,"deviceId","1.0")valfileLocation=Environment.getExternalStorageDirectory().absolutePathvalclientResources=ClientResources(okHttpClient!!,settingsParameters!!)clientResources.downloadResource("nameofresource","1.0","$fileLocation/filename.ext",true,object: ClientResources.CallbackListener<Void>{overridefunonSuccess(v:Void){//Handle success callback}overridefunonError(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
SettingsParameterssettingsParameters=newSettingsParameters(hostUrl,APP_ID,"deviceId","1.0");ClientResourcesclientResources=newClientResources(okHttpClient,settingsParameters);clientResources.fetchResourceInfo(newClientResources.CallbackListener<ResourceBundle[]>(){@OverridepublicvoidonSuccess(ResourceBundle[]info){//Handle success callbackfor(inti=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");}}@OverridepublicvoidonError(@NonNullThrowableerror){//Handle the error callback}});