我想将我的Android应用程序连接到远程MongoDB数据库:
try {
String str = "";
MongoClientURI uri = new MongoClientURI("mongodb://byulent:mypass@ds231549.mlab.com:31549/mydb");
MongoClient mongoClient = new MongoClient(uri);
MongoDatabase db = mongoClient.getDatabase(uri.getDatabase());
MongoCollection<Document> photos = db.getCollection("photos");
FindIterable<Document> cursor = photos.find();
Iterator i = cursor.iterator();
while (i.hasNext()){
Log.d("obj", i.next().toString());
}
//Toast.makeText(context, "Succesfully connected to MongoDB", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.e("mongodb", e.getMessage());
}
但是当我尝试从DB获取数据时,我看到了这个错误:
E/mongodb: Timed out after 30000 ms while waiting to connect. Client view of cluster state
is {type=UNKNOWN, servers=[{address=ds231549.mlab.com:31549, type=UNKNOWN, state=CONNECTING,
exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by
{java.net.SocketTimeoutException: failed to connect to ds231549.mlab.com/34.245.70.193
(port 31549) after 20000ms}}]
为什么会出现此错误以及如何解决?