1

编辑

我完成了它,因为我只是错过了包含 app_key % app_secret 密钥的 kinvey.properties 文件。

我正在尝试在 Kinvey Android TestDrive App 上使用离线 Appdata(ONLINE_FIRST 策略)功能。如果连接可用,应用程序会直接将数据保存到后端,但它仍然给我一个关于用于无连接模式的后台服务意图的错误。像这样的东西。

    2060-2073/com.example.testdrive.android:backgroundsync E/AndroidRuntime﹕ FATAL EXCEPTION: IntentService[Kinvey Sync Service]
Process: com.example.testdrive.android:backgroundsync, PID: 2060
java.lang.NullPointerException
        at com.kinvey.java.AbstractClient$Builder.loadPropertiesFromDisk(AbstractClient.java:353)
        at com.kinvey.android.Client$Builder.<init>(Client.java:592)
        at com.kinvey.android.offline.AbstractSyncService.initClientAndKickOffSync(AbstractSyncService.java:94)
        at com.kinvey.android.offline.AbstractSyncService.onHandleIntent(AbstractSyncService.java:61)
        at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.os.HandlerThread.run(HandlerThread.java:61)

onSaveClick 方法发送保存请求。

测试活动

package com.example.testdrive.android;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.example.testdrive.android.model.Entity;
import com.kinvey.android.AsyncAppData;
import com.kinvey.android.Client;
import com.kinvey.android.callback.KinveyDeleteCallback;
import com.kinvey.android.callback.KinveyListCallback;
import com.kinvey.android.callback.KinveyUserCallback;
import com.kinvey.android.offline.SqlLiteOfflineStore;
import com.kinvey.java.Query;
import com.kinvey.java.User;
import com.kinvey.java.core.KinveyClientCallback;
import com.kinvey.java.model.KinveyDeleteResponse;
import com.kinvey.java.offline.OfflinePolicy;

import java.util.Calendar;

public class TestDrive extends Activity {

    public static final String TAG = "TestDrive";

    private ProgressBar bar;

    private String mydate;

    private String appKey="kid_VeZoHqKOZ9";
    private String appSecret="8311c26bf1f546c785aa894da1139f3e";

    private Client kinveyClient;
    private AsyncAppData<Entity> ad;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_drive);
        bar = (ProgressBar) findViewById(R.id.refresh_progress);
        bar.setIndeterminate(true);
        kinveyClient = new Client.Builder(appKey, appSecret, TestDrive.this).build();
        if (!kinveyClient.user().isUserLoggedIn()) {
            bar.setVisibility(View.VISIBLE);
            kinveyClient.user().login(new KinveyUserCallback() {
                @Override
                public void onSuccess(User result) {
                    bar.setVisibility(View.GONE);
                    Log.i(TAG,"Logged in successfully as " + result.getId());
                    Toast.makeText(TestDrive.this, "New implicit user logged in successfully as " + result.getId(),
                            Toast.LENGTH_LONG).show();
                }
                @Override
                public void onFailure(Throwable error) {
                    bar.setVisibility(View.GONE);
                    Log.e(TAG, "Login Failure", error);
                    Toast.makeText(TestDrive.this, "Login error: " + error.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
        }   else {
            Toast.makeText(this, "Using cached implicit user " + kinveyClient.user().getId(), Toast.LENGTH_LONG).show();
        }

        ad = kinveyClient.appData("entityCollection", Entity.class);
        ad.setOffline(OfflinePolicy.ONLINE_FIRST, new SqlLiteOfflineStore<Entity>(this));

    }
    public void onSaveClick(View view) {
        bar.setVisibility(View.VISIBLE);
        mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
        Entity entity = new Entity("myEntity"+mydate
        );
        entity.put("Description","This is a description of a dynamically-added Entity property.");
        ad.save(entity, new KinveyClientCallback<Entity>() {
            @Override
            public void onSuccess(Entity result) {
                bar.setVisibility(View.GONE);
                Toast.makeText(TestDrive.this, "Entity Saved\nTitle: " + result.getTitle()
                        + "\nDescription: " + result.get("Description"), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(Throwable error) {
                bar.setVisibility(View.GONE);
                Log.e(TAG, "AppData.save Failure", error);
                Toast.makeText(TestDrive.this, "Save All error: " + error.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
    }

}

KinveySyncService 不会被触发。

Android主程序

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testdrive.android"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name="com.example.testdrive.android.TestDrive"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
            android:name="com.kinvey.android.offline.KinveySyncService"
            android:exported="true" android:process=":backgroundsync" >
            <intent-filter>
                <action android:name="com.kinvey.android.ACTION_OFFLINE_SYNC" />
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.STATE_CHANGE" />
            </intent-filter>
        </service>
    </application>



</manifest>

谢谢。

4

0 回答 0