使用 android + mongodb 时出现 java.net.UnknownHostException : Unable to resolve host "@MYIPADDRESS,

标签 java android mongodb android-asynctask

我创建了一个像这样的 MongoClientURI:

MongoClientURI uri = new MongoClientURI( "mongodb://User_name:Password@MyIPAddress:27017/"+TEST_SKETCH_APP );

        MongoClient instance = MongoDatabaseConnection.getInstance(uri);
        DB db = instance.getDB(uri.getDatabase());
        DBCollection collection = db.getCollection(SKETCH_COLLECTION);

        BasicDBObject  dbObject = new BasicDBObject();

        dbObject.put("name", url[0]);
        dbObject.put("password", url[1]);
        dbObject.put("email", url[2]);
        dbObject.put("phoneNumber", url[3]);

        collection.insert(dbObject);

我从 AsyncTask 调用这个方法:

@Override
    protected JSONObject doInBackground(String... urls) {

        // params comes from the execute() call: params[0] is the url.
        try {
            return saveValues(urls);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return  null;
    }

我也拥有互联网连接所需的所有权限:

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

但我仍然无法将值插入到在该 IP 地址中运行的数据库中。 完整的错误说:

Timed out after 10000 ms while waiting for a server that matches AnyServerSelector{}.
Client view of cluster state is {type=Unknown, servers=[{address=@MYIPADDRESS:27017,
 type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: 
 Exception opening the socket}, caused by {java.net.UnknownHostException: 
 Unable to resolve host "@MYIPADDRESS": No address associated with hostname}, 
 caused by {android.system.GaiException: android_getaddrinfo failed: 
 EAI_NODATA (No address associated with hostname)}}]

我知道 IP 地址是正确的,因为我可以在 chrome 中输入此地址 http://MYIPADDRESS:27017/

并得到了返回:

您似乎正在尝试在 native 驱动程序端口上通过 HTTP 访问 MongoDB。

最佳答案

FWIW我遇到了同样的错误。我试图静态连接,但您需要在构造函数中执行此操作。

关于使用 android + mongodb 时出现 java.net.UnknownHostException : Unable to resolve host "@MYIPADDRESS,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40549919/

相关文章:

java - 加密 zip

android - ImageButtons 数组,从变量分配 R.view.id

java - 在Kotlin中,有没有一种方法可以通过函数调用将更多的值添加到枚举中?

node.js - MongoDB 更新到 3.0 后 Heroku 应用程序崩溃

java - 从网络获取运行时类信息?类似于 javap 但在运行时加载到内存中的类

Java 查找数组中的所有组合,这些组合加起来等于特定数字

android - DownloadManager 进度条(带百分比)

mongodb - "mongo"和 "mongod"之间的区别?

java - 如何让 MongoCollectoin 返回 java.sql.Timestamp?

java - 如何使用适配器在 Android ListView 中显示 firestore 集合