简体   繁体   English

Android mongodb.stitch API调用无法获取RemoteMongoClient

[英]Android mongodb.stitch API call fails to get RemoteMongoClient

I have followed the steps for posting data to Mongo db using Android application. 我已经按照使用Android应用程序将数据发布到Mongo数据库的步骤进行了操作。

Link: 链接:

https://code.tutsplus.com/tutorials/how-to-use-mongodb-stitch-in-android-apps--cms-31877 https://code.tutsplus.com/tutorials/how-to-use-mongodb-stitch-in-android-apps--cms-31877

But the code fails to get the remote client object. 但代码无法获取远程客户端对象。

Here is the code snippet which fails: 以下是失败的代码段:

private void logIN(){
        final StitchAppClient mongoClient1 = Stitch.getDefaultAppClient();
        mongoClient1.getAuth().loginWithCredential(new AnonymousCredential()).addOnSuccessListener(new OnSuccessListener<StitchUser>() {
            @Override
            public void onSuccess(StitchUser stitchUser) {
                Log.v(TAG,"success");
                try {
                    RemoteMongoClient remoteMongoClient =
                            client.getServiceClient(RemoteMongoClient.factory, "mongodb-atlas");

                    // Set up the atlas collection
                    RemoteMongoCollection<Document> coll = remoteMongoClient
                            .getDatabase(DATABASE_NAME).getCollection(COLLECTION_NAME);
                }catch (Exception e){
                    e.getMessage();
                }
               // post();
            }
        });
    }

This method fails at the API call getServiceClient. 此方法在API调用getServiceClient时失败。

Here is the logcat error: 这是logcat错误:

04-17 15:36:39.478 22667-23111/com.ecom.analyticsmongo E/AndroidRuntime: FATAL EXCEPTION: Thread-5
    Process: com.ecom.analyticsmongo, PID: 22667
    java.lang.BootstrapMethodError: Exception from call site #0 bootstrap method
        at com.mongodb.stitch.android.services.mongodb.remote.RemoteMongoClient.<clinit>(RemoteMongoClient.java:37)
        at com.ecom.analyticsmongo.MainActivity$2$1.run(MainActivity.java:61)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: java.lang.ClassCastException: Bootstrap method returned null
        at com.mongodb.stitch.android.services.mongodb.remote.RemoteMongoClient.<clinit>(RemoteMongoClient.java:37) 
        at com.ecom.analyticsmongo.MainActivity$2$1.run(MainActivity.java:61) 
        at java.lang.Thread.run(Thread.java:764) 

Let me what could be the cause for this failure. 让我知道造成这种失败的原因。 Or if anyone has faced such runtime error 或者,如果有人遇到这样的运行时错误

The issue was with the gradle setting for JAVA version. 问题在于JAVA版本的gradle设置。 We need to define this in projects gradle: 我们需要在项目gradle中定义它:

compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

After defining this, I could see the code working fine now. 定义之后,我可以看到代码现在工作正常。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM