Skip to Content

Step by Step with the SAP Cloud Platform SDK for Android

Part 6 — Offline OData

Previous (Online OData)   Next (Remembering Credentials)

A mobile application may not always be used in areas where a network connection is present or if it is, it may have high latency. The Offline OData framework enables the application to work against a local database instead of making online requests to read or update data.

The following are some additional sources of documentation on this topic.
Introduction to Offline API
Offline Applications Overview (SCPms docs)
Defining an Application Configuration File

The following steps demonstrate how to enable the app to access data from a local database enabling it to work without network connectivity.

    1. Modify the app’s build.gradle to include the Offline OData library.
      implementation 'com.sap.cloud.android:offline-odata:1.1.100'
    2. Modify the app’s build.gradle to include the following in the android defaultConfig section:
      ndk {
          //exclude 64 bit libraries as offline library is 32 bit only
          abiFilters "armeabi-v7a", "x86"
      }
    3. Add the following variables to MainActivity.
      private ESPMContainer myOfflineServiceContainer;
      private OfflineODataProvider myOfflineDataProvider;
      
    4. Add the following method.
      private void setupOfflineOData() {
          Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("com.sap.cloud.mobile.odata");
          //logger.setLevel(Level.ALL);
          AndroidSystem.setContext(getApplicationContext());
      
          try {
              URL url = new URL(serviceURL + "/" + connectionID);
              OfflineODataParameters offParam = new OfflineODataParameters();
              offParam.setEnableRepeatableRequests(false);
              myOfflineDataProvider = new OfflineODataProvider(url, offParam, myOkHttpClient, null, null);
              OfflineODataDefiningQuery myDefiningQuery = new OfflineODataDefiningQuery("Products", "Products", false);
              myOfflineDataProvider.addDefiningQuery(myDefiningQuery);
          } catch (OfflineODataException e) {
              e.printStackTrace();
          }
          catch (MalformedURLException e) {
              e.printStackTrace();
          }
      
          myOfflineDataProvider.open(() -> {
              Log.d(myTag, "Offline store opened");
              toastAMessage("Offline store opened");
              final Button offlineODataButton = (Button) findViewById(R.id.b_odataOffline);
              Handler handler = new Handler(Looper.getMainLooper());
              handler.post(new Runnable() {
                  @Override
                  public void run() {
                      offlineODataButton.setEnabled(true);
                  }
              });
              myOfflineServiceContainer = new ESPMContainer(myOfflineDataProvider);
      
          }, (OfflineODataException offlineOdataException) -> {
              Log.d(myTag, "Offline store did not open.", offlineOdataException);
              toastAMessage("Offline store failed to open.  " + offlineOdataException.getMessage());
          });
      }
      

      As the above uses Lambda expressions, it requires setting the source and target compatibility to 1.8.

    5. In the onRegister method in the updateUICallback add the below code to the onFailure and onResponse methods as shown below. If the app is started and there is no network connection, onFailure is called.
      setupOfflineOData();

    6. Add the following code to the onODataOffline method.
      if (myOfflineServiceContainer != null) {
          List<Product> products = myOfflineServiceContainer.getProducts();
          toastAMessage(products.size() + " products returned");
          for (Product product : products) {
              Log.d(myTag, product.getName());
          }
      }
      else {
          Log.d(myTag, "Offline service container is null.");
          toastAMessage("Offline service container is null.");
      }
      
    7. Run the example with a network connection. The first time the store is opened, a database will be created on the server and downloaded to the device. The OData Offline button will become enabled once the store has been opened. The results of pressing the OData Offline button are shown below.
    8. Turn on airplane mode and click on the OData Offline button and notice that the data is successfully returned as the query is fulfilled from the offline store on the device.

Previous (Online OData)   Next (Remembering Credentials)

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply