I have followed the steps for posting data to Mongo db using Android application.

Link:

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.

Here is the logcat error:

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

1 Answers

0
Rajan On Best Solutions

The issue was with the gradle setting for JAVA version. We need to define this in projects gradle:

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

After defining this, I could see the code working fine now.

Thanks