使用SAP OData offline库实现Android应用的离线(offline)模式
生活随笔
收集整理的這篇文章主要介紹了
使用SAP OData offline库实现Android应用的离线(offline)模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
打開Android studio,在工程的build.gradle里加入下面的依賴,導入SAP OData offline庫:
implementation group:‘com.sap.cloud.android’, name:‘offline-odata’, version: sdkVersion
點擊Sync now:
新建一個java文件:SAPServiceManager.java
使用的import如下:
import android.content.Context; import android.util.Log; import android.widget.Toast; import com.sap.cloud.mobile.odata.core.AndroidSystem; import com.sap.cloud.mobile.odata.offline.OfflineODataDefiningQuery; import com.sap.cloud.mobile.odata.offline.OfflineODataException; import com.sap.cloud.mobile.odata.offline.OfflineODataParameters; import com.sap.cloud.mobile.odata.offline.OfflineODataProvider; import java.net.MalformedURLException; import java.net.URL;// 將OfflineODataProvider注釋掉,改為OfflineODataProvider //private OnlineODataProvider provider; private OfflineODataProvider provider; private static final String TAG = SAPServiceManager.class.getName();OData offiline datastore的setup方法:public void openODataStore(Action0 callback, Context context) {setupOfflineOData(callback, context); }private void setupOfflineOData(Action0 callback, Context context) {try {if (configurationData.loadData()) {Toast.makeText(context, "Opening the offline store which may take a few moments the first time it is opened.", Toast.LENGTH_LONG).show();//Initialize application context for use by OfflineODataProviderAndroidSystem.setContext(context);//commonly set parameters include setStoreEncryptionKey, setStoreName, setStorePathOfflineODataParameters offParam = new OfflineODataParameters();String serviceUrl = configurationData.getServiceUrl();URL url = new URL(serviceUrl + CONNECTION_ID_ESPMCONTAINER);provider = new OfflineODataProvider(url, offParam, ClientProvider.get());//Specifies which entities we wish to include in the offline storeprovider.addDefiningQuery(new OfflineODataDefiningQuery("Customers", "Customers", false));}} catch (MalformedURLException e) {e.printStackTrace();} catch (OfflineODataException e) {e.printStackTrace();}//opens the offline store. This can take a minute or so the first time it is called as it is created and populated.Log.d(TAG, "About to call open on the offline store.");provider.open(() -> {Log.d(TAG, "Offline store opened.");eSPMContainer = new ESPMContainer(provider);callback.call();syncOfflineData(); //TODO could be triggered via a menu action}, (OfflineODataException offlineODataException) -> {Log.d(TAG, "Offline store did not open.", offlineODataException);}); }public void syncOfflineData() {//send the local changes to the back end OData serviceLog.d(TAG, "About to call provider.upload.");provider.upload(() -> {Log.d(TAG, "Successfully uploaded the changed data.");//get changes from the back end OData service.provider.download(() -> {Log.d(TAG, "Successfully downloaded the changed data.");}, (error) -> {Log.d(TAG, "Failed when downloading the changed data with error: " + error.getMessage());});}, (error) -> {Log.d(TAG, "Failed when uploading the changed data with error: " + error.getMessage());}); }public String getServiceRoot() {if (configurationData.loadData()) {String serviceUrl = configurationData.getServiceUrl();if (serviceRoot == null) {serviceRoot = serviceUrl + "/" + CONNECTION_ID_ESPMCONTAINER;}}else {throw new IllegalStateException("SAPService manager configurationData.loadData() failed.");}return serviceRoot; }新建LogonActivity.java,調用之前實現的sapServiceManager.openODataStore:
第一次運行應用,會發現offline OData store的初始化日志:
現在可以測試離線模式了,打開移動設備的飛行模式:
選擇存儲于OData offline store上的某個客戶,點擊編輯按鈕:
修改city字段:
關閉該應用,關閉設備的飛行模式,再啟動該應用,在logcat里能觀察到之前處于離線模式修改的數據已經自動同步到了后臺online store里:
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
總結
以上是生活随笔為你收集整理的使用SAP OData offline库实现Android应用的离线(offline)模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何使用SAP云平台的Notificat
- 下一篇: AMD Radeon RX 7700/7