本篇文章給大家分享的是有關如何使用SAP OData offline庫實現Android應用的離線模式,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
成都創新互聯公司專注于企業成都全網營銷推廣、網站重做改版、銀川網站定制設計、自適應品牌網站建設、H5頁面制作、電子商務商城網站建設、集團公司官網建設、成都外貿網站建設公司、高端網站制作、響應式網頁設計等建站業務,價格優惠性價比高,為銀川等各大城市提供網站開發制作服務。
打開Android studio,在工程的build.gradle里加入下面的依賴,導入SAP OData offline庫:
點擊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 OfflineODataProvider AndroidSystem.setContext(context); //commonly set parameters include setStoreEncryptionKey, setStoreName, setStorePath OfflineODataParameters 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 store provider.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 service Log.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里:
以上就是如何使用SAP OData offline庫實現Android應用的離線模式,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注創新互聯行業資訊頻道。
標題名稱:如何使用SAPODataoffline庫實現Android應用的離線模式
本文路徑:http://vcdvsql.cn/article22/jhjejc.html
成都網站建設公司_創新互聯,為您提供App開發、電子商務、App設計、關鍵詞優化、定制網站、微信小程序
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯