如何使用android应用程序连接远程mongo db

时间:2016-08-11 11:48:15

标签: android mongodb

我想在我的Android应用和mongodb之间建立连接。这是我的代码:

    try {
        //MongoClient mongoClient = new MongoClient("10.1.1.1", 27017);//new MongoClient("mongodb://10.1.1.1:27017");
        String dbURI = "mongodb://192.168.1.2:27017";
        MongoClient mongoClient = new MongoClient(new MongoClientURI(dbURI));

        DB db = mongoClient.getDB("TestDatabase");
        Log.d(LOG_TAG, "DBName = " + db.getName());

        Set<String> collections = db.getCollectionNames();
        for (String colName : collections) {
            Log.d(LOG_TAG, "colName = " + colName);
            System.out.println("\t + Collection: " + colName);
        }

        mongoClient.close();
    }
    catch (Exception e){
        e.printStackTrace();
        Log.d(LOG_TAG, "exceptionMongo = " + e);
    }

和gradle似乎是这样的

compile 'org.mongodb:mongo-java-driver:2.13.2'

和清单文件就像

 <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

请告诉我,我做错了什么?

0 个答案:

没有答案