将Parse服务器添加到现有项目中

时间:2016-10-15 20:15:20

标签: android parse-platform parse-server

我有一个项目,我想将Parse添加到它。我在我的电脑上安装了Parse服务器,从他们的网站下载了SDK,工作得很好 但是,我不想在他们的网站上使用SDK,我只想将库添加到我的项目中。

这是我做的:我将这些库文件复制到我的项目libs文件夹

'libs/parse-android-1.13.1.jar'
'libs/bolts-android-1.2.0.jar'

这是我的代码:

build.gradle文件中的

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'

    compile files('libs/parse-android-1.13.1.jar')
    compile files('libs/bolts-android-1.2.0.jar')

}

我添加了上面的依赖项。

主要活动:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Parse.initialize(new Parse.Configuration.Builder(getApplicationContext())
                .applicationId("kooora.com100plus")
                .clientKey("kooora.com100plusMasterKey")
                .server("http://10.0.2.2:1337/parse/")
                .build()
        );


        ParseObject gameScore = new ParseObject("GameScore");
        gameScore.put("score", 1337);
        gameScore.put("playerName", "Sean Plott");
        gameScore.put("cheatMode", false);
        gameScore.saveInBackground(new SaveCallback() {
            public void done(ParseException e) {
                if (e == null) {
                    Log.i("Parse", "Save Succeeded");
                } else {
                    Log.i("Parse", e.getMessage());
                }
            }
        });
    }
}

以下是我收到错误的日志:

10-16 06:34:42.539 25428-25428/? I/art: Not late-enabling -Xcheck:jni (already on)
10-16 06:34:42.563 25428-25436/? E/art: Failed writing handshake bytes (-1 of 14): Broken pipe
10-16 06:34:42.563 25428-25436/? I/art: Debugger is no longer active
10-16 06:34:42.696 25428-25428/? D/AndroidRuntime: Shutting down VM  
10-16 06:34:42.696 25428-25428/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sabirmoglad.big_boss, PID: 25428
java.lang.NoClassDefFoundError: Failed resolution of: Lbolts/TaskCompletionSource;
   at com.parse.ParseTaskUtils.callbackOnMainThreadAsync(ParseTaskUtils.java:100)
   at com.parse.ParseTaskUtils.callbackOnMainThreadAsync(ParseTaskUtils.java:72)
   at com.parse.ParseTaskUtils.callbackOnMainThreadAsync(ParseTaskUtils.java:59)
   at com.parse.ParseObject.saveInBackground(ParseObject.java:1529)
   at com.example.sabirmoglad.big_boss.MainActivity.onCreate(MainActivity.java:31)
   at android.app.Activity.performCreate(Activity.java:5937)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
   at android.app.ActivityThread.access$800(ActivityThread.java:144)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:135)
   at android.app.ActivityThread.main(ActivityThread.java:5221)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.ClassNotFoundException: Didn't find class "bolts.TaskCompletionSource" on path: DexPathList[[zip file "/data/app/com.example.sabirmoglad.big_boss-2/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
   at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
   at com.parse.ParseTaskUtils.callbackOnMainThreadAsync(ParseTaskUtils.java:100) 
   at com.parse.ParseTaskUtils.callbackOnMainThreadAsync(ParseTaskUtils.java:72) 
   at com.parse.ParseTaskUtils.callbackOnMainThreadAsync(ParseTaskUtils.java:59) 
   at com.parse.ParseObject.saveInBackground(ParseObject.java:1529) 
   at com.example.sabirmoglad.big_boss.MainActivity.onCreate(MainActivity.java:31) 
   at android.app.Activity.performCreate(Activity.java:5937) 
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
   at android.app.ActivityThread.access$800(ActivityThread.java:144) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:135) 
   at android.app.ActivityThread.main(ActivityThread.java:5221) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:372) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
    Suppressed: java.lang.ClassNotFoundException: bolts.TaskCompletionSource
   at java.lang.Class.classForName(Native Method)
   at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
   at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
        ... 19 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

我不知道出了什么问题!我只是得到一个运行时错误,它停止工作

更新:

正如评论中所建议的那样,我将初始化放在我的Application类中 实际上我从原始SDK复制了类,但似乎这个初始化没有执行,

这是我从原始SDK中添加的类:

public class StarterApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        // Enable Local Datastore.
        Parse.enableLocalDatastore(this);

        // Add your initialization code here
        Parse.initialize(new Parse.Configuration.Builder(getApplicationContext())
                .applicationId("kooora.com100plus")
                .clientKey("kooora.com100plusMasterKey")
                .server("http://10.0.2.2:1337/parse/")
        .build()
        );

          ParseUser.enableAutomaticUser();
        ParseACL defaultACL = new ParseACL();
        // Optionally enable public read access.
        // defaultACL.setPublicReadAccess(true);
        ParseACL.setDefaultACL(defaultACL, true);
    }

}

1 个答案:

答案 0 :(得分:1)

首先,没有必要将这两个libs行添加到Gradle文件中。这一行包含所有JAR文件。

    Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref m_CurrentVelocity, damping);
    Vector3 newPos2 = new Vector3(newPos.x, newPos.y + 1, newPos.z);
    transform.position = newPos2;

现在,我通常建议您在JCenter或Maven Central中找到依赖项时尽量避免使用JAR库(如果您不确定,请查看这些库)。原因是 - 为您提供了传递依赖,版本管理需要进行一些数字更改,而不是覆盖文件等。

然后,Class not found错误可能源于使用过时版本的库。

总而言之,删除JAR文件,使用这些依赖项编译库。 (上述评论中的最新版本)

compile fileTree(dir: 'libs', include: ['*.jar'])