App42安卓离线存储无法正常工作

时间:2016-06-19 11:09:20

标签: android app42 offline-storage

我正在创建一个Android应用程序,我想使用离线存储。所以,我已经创建了测试应用程序以测试app42 BaaS。 App42在TestApplication.java中初始化。应用程序必须将信息保存在脱机存储中(有关JSON字符串中的“Tonya”的信息),并且当Internet连接可用时,它必须将此信息发送到在线数据库。好吧,根据日志,“Tonya”是离线写的,但是当我使用“findAllDocuments”时它不会出现。当互联网连接可用时,它不会与在线数据库同步。我尝试使用不同的Android和SDK版本,但没有帮助。我做错了什么?

这是AndroidManifest.xml:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.luka94.testapplication">
    <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>

这是TestApplication.java:

package com.luka94.testapplication;

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 = "DBNAME";

    @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;}

}

这是MainActivity.java:

package com.luka94.testapplication;

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());
            }
        });
    }

}

1 个答案:

答案 0 :(得分:0)

App42离线存储空间存储类型为 GET 的API调用的响应。当再次进行相同的API调用时,根据您设置的策略,将向您发送响应,该响应可能来自服务器或缓存。仅当方法签名相同时才会起作用。它缓存该请求的响应并传递其下一个呼叫。如果要进行不同的API调用以检索相应的数据,则不会获得响应数据。您还可以查看其Documentation以获取更多信息。

我希望它会有所帮助。