zoukankan      html  css  js  c++  java
  • 使用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 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里:

    要获取更多Jerry的原创文章,请关注公众号"汪子熙":

  • 相关阅读:
    Python遇到的零碎小问题
    【bug】YYC松鼠短视频系统V2.7版本以上 增加阿里云上传后,“上传视频无法生成缩略图报错” 修改
    【补丁】YYC松鼠短视频系统补丁,增加视频点赞数据管理功能,可修改点赞数量,V2.8的功能
    【bug】修复YYC松鼠短视频系统V2.7版本bug 注册输入验证码提示邀请码,输入邀请码提示错误
    YYC松鼠短视频系统【bug】短信验证码功能bug,新注册短信用户任意填写验证码都能通过注册的严重bug修复
    修复YYC松鼠短视频系统我的收藏页面 没有返回按钮的bug
    学好C++必须要注意的十八个问题
    es节点失效,手动重置primary,迁移分区
    elasticsearch 大集群最基本,也是最重要的两个配置gc和指针压缩
    2020年一整年几乎没有更新,重新开张
  • 原文地址:https://www.cnblogs.com/sap-jerry/p/12444264.html
Copyright © 2011-2022 走看看