App42 Offline Storage Android

0 votes

Hello, I’m creating an Android Application and I want to use the offline storage. So, I’ve created test application to test app42 BaaS. App42 was initialized in TestApplication.java. App must save the information in offline storage (information about “Tonya” in JSON string) and when Internet connection is available, it must send this information to the online DB. Well, according to the log, “Tonya” was written offline, but it does not appear when I use “findAllDocuments”. And when the internet connection became available, it didn’t synchronize with the online DB. I've tried to use different Android and SDK versions, it didn’t help. What am I doing wrong?
 

This is AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.luka94.firebasetestapplication">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS"/>

    <application
        android:name=".TestApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name="com.shephertz.app42.paas.sdk.android.App42BroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
        <receiver android:name="com.shephertz.app42.paas.sdk.android.AlarmReceiver"/>
        <service android:name="com.shephertz.app42.paas.sdk.android.App42DataSyncService"/>
    </application>

</manifest>

This is TestApplication.java:
 

package com.luka94.firebasetestapplication;

import android.app.Application;

import com.shephertz.app42.paas.sdk.android.App42API;
import com.shephertz.app42.paas.sdk.android.App42CacheManager;
import com.shephertz.app42.paas.sdk.android.App42Response;
import com.shephertz.app42.paas.sdk.android.App42Exception;
import com.shephertz.app42.paas.sdk.android.App42BadParameterException;
import com.shephertz.app42.paas.sdk.android.App42NotFoundException;
import com.shephertz.app42.paas.sdk.android.App42CallBack;
import com.shephertz.app42.paas.sdk.android.storage.StorageService;
import com.shephertz.app42.paas.sdk.android.user.User;
import com.shephertz.app42.paas.sdk.android.user.User.Profile;
import com.shephertz.app42.paas.sdk.android.user.User.UserGender;
import com.shephertz.app42.paas.sdk.android.user.UserService;

public class TestApplication extends Application {

    private UserService userService;
    private StorageService storageService;
    public static final String DB_NAME = "ATINGOOODB";

    @Override
    public void onCreate() {
        super.onCreate();
        App42API.initialize(getApplicationContext(), "KEY_1", "KEY_2");
        App42CacheManager.setPolicy(App42CacheManager.Policy.NETWORK_FIRST);
        App42CacheManager.setExpiryInMinutes(60);
        userService = App42API.buildUserService();
        storageService = App42API.buildStorageService();
        App42API.setOfflineStorage(true);
    }

    public UserService getUserService() {
        return userService;
    }
    public StorageService getStorageService() {return storageService;}

}

This is MainActivity:
 

package com.luka94.firebasetestapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.shephertz.app42.paas.sdk.android.App42API;
import com.shephertz.app42.paas.sdk.android.App42CallBack;
import com.shephertz.app42.paas.sdk.android.storage.Storage;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private TestApplication mApplication;
    private String json;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mApplication = (TestApplication) getApplication();

        json = "{\"name\":\"Tonya\",\"age\":\"28\",\"phone\":\"xxx-xxx-xxx\"}";
        testWriteData();
        getRecord();
    }

    private void getRecord() {
        mApplication.getStorageService().findAllDocuments(TestApplication.DB_NAME,
                "test", new App42CallBack() {
                    @Override
                    public void onSuccess(Object o) {
                        ArrayList<Storage.JSONDocument> jsonDocList = ((Storage) o).getJsonDocList();

                        if (!jsonDocList.isEmpty()) {
                            Log.d("TestApp", "objects finded");

                            for (int i = 0; i < jsonDocList.size(); i++) {
                                Log.d("TestApp", String.format("%s", jsonDocList.get(i).toString()));
                            }
                        }
                        Log.d("TestApp", String.format("is from online storage: %b", ((Storage) o).isOfflineSync()));
                        Log.d("TestApp", String.format("is from cache: %b", ((Storage) o).isFromCache()));
                    }

                    @Override
                    public void onException(Exception e) {
                        Log.d("TestApp", e.getMessage());
                    }
                });
    }

    private void testWriteData() {
        mApplication.getStorageService().insertJSONDocument(TestApplication.DB_NAME, "test", json, new App42CallBack() {
            @Override
            public void onSuccess(Object response) {
                Storage storage = (Storage) response;
                if (storage.isOfflineSync()) {
                    Log.d("TestApp", "writed offline");
                } else {
                    Log.d("TestApp", "writed online");
                }
            }

            @Override
            public void onException(Exception ex) {
                System.out.println("Exception Message : " + ex.getMessage());
            }
        });
    }

}
asked Jun 18, 2016 in Android by Саша Туровский (10 points)

1 Answer

0 votes
Hi Cama,

Greetings!!!!!!!

It will works only in case you have same request signature. It will not be able to work if the API call method is change. while fetching the data it will not able to caching of the API.

Let me know if more queries are concern.

Thanks

Vishnu Garg
answered Jun 20, 2016 by Vishnu Garg (674 points)
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...