这就是我将 Java 驱动程序添加到我的项目的方式
compile 'org.mongodb:mongo-java-driver:2.13.2'
在我运行我的 Android 应用程序 Gradle 之后。如果我直接添加罐子,我的应用程序无论如何都会崩溃。
连接代码如下:
MongoClient mongoClient = new MongoClient("mongodb://Dbuser:dbpass@ds047692.mongolab.com:47692");
DB db = mongoClient.getDB("testdb");
Set<String> collectionNames = db.getCollectionNames();
我只需要从我的应用程序连接到 mongo 数据库。我不确定我的连接字符串的正确性。在我的数据库帐户中,我发现:
通过标准 URI 使用驱动程序连接:mongodb://blablauser:password@ds047692.mongolab.com:47692/urdb
但是在代码中它没有连接。
我使用 mongo-java-driver。但是每次我尝试阅读我的数据库时,我都会得到
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.artem.mongodb/com.example.artem.mongodb.MainActivity}: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=ds047692.mongolab.com:47692, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketException: socket failed: EACCES (Permission denied)}, caused by {android.system.ErrnoException: socket failed: EACCES (Permission denied)}}]
Document myDoc = collection.find().first();//crash point
为什么我无法访问自己的数据库?