0

我正在创建一个 Android 应用程序,我想使用离线存储。因此,我创建了测试应用程序来测试 app42 BaaS。App42 在 TestApplication.java 中初始化。应用程序必须将信息保存在离线存储中(JSON 字符串中有关“Tonya”的信息),并且当互联网连接可用时,它必须将此信息发送到在线数据库。嗯,根据日志,“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());
            }
        });
    }

}
4

1 回答 1

0

App42离线存储存储GET类型的 API 调用的响应。当基于您设置的策略再次进行相同的 API 调用时,将向您发送响应,响应可能来自服务器或缓存。只有当方法签名相同时它才会起作用。它缓存该请求的响应并为下一次调用提供服务。如果您进行不同的 API 调用来检索相应的数据,您将不会获得响应数据。您还可以查看其文档以获取更多信息。

我希望它会有所帮助。

于 2016-08-04T05:36:10.860 回答