我无法将Android应用程序连接到MongoDB

时间:2019-12-17 22:14:46

标签: java android mongodb android-studio

我有一个错误,我什至都不知道该怎么读或出了什么问题,这是代码:


import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;



public class MainActivity extends AppCompatActivity {
    TextView dataBase;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dataBase = (TextView) findViewById(R.id.db);
        MongoClientURI uri = new MongoClientURI(
                "mongodb+srv://Razvan:justforfun@cluster0-27vbb.mongodb.net/test?retryWrites=true&w=majority");

        MongoClient mongoClient = new MongoClient(uri);
        MongoDatabase database = mongoClient.getDatabase("Location");
        MongoIterable<String> collections = database.listCollectionNames();
        for (String collectionName: collections) {
            System.out.println(collectionName);
            dataBase.setText(collectionName);
    }
    }}

这就是我的错误:

2019-12-15 17:51:52.734 29344-29344/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2019-12-15 17:51:53.302 29344-29344/com.example.testaddtodb E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.testaddtodb, PID: 29344
    java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/naming/directory/InitialDirContext;
        at com.mongodb.internal.dns.DnsResolver.createDnsDirContext(DnsResolver.java:140)
        at com.mongodb.internal.dns.DnsResolver.resolveHostFromSrvRecords(DnsResolver.java:58)
        at com.mongodb.ConnectionString.<init>(ConnectionString.java:325)
        at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:238)
        at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:220)
        at com.example.testaddtodb.MainActivity.onCreate(MainActivity.java:55)
        at android.app.Activity.performCreate(Activity.java:7183)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.naming.directory.InitialDirContext" on path: DexPathList[[zip file "/data/app/com.example.testaddtodb-pUt_yDyfjwK1W3EqtvcFaw==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.testaddtodb-pUt_yDyfjwK1W3EqtvcFaw==/lib/arm64, /system/lib64, /system/vendor/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        ...

“ MongoClientURI uri ...”怎么了? 我想将我的Android应用程序连接到MongoDB测试数据库,但出现此错误,我什至不知道该怎么读...

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以尝试this answer on mongodb forum