繁体   English   中英

java.net.UnknownHostException:无法解析主机“@MYIPADDRESS,当使用android + mongodb时

[英]java.net.UnknownHostException: Unable to resolve host "@MYIPADDRESS, when using android + mongodb

我创建了一个像这样的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 ADDRESS是正确的,因为我可以在chrome http://MYIPADDRESS:27017/

得到这个回报:

It looks like you are trying to access MongoDB over HTTP on the native driver port.

FWIW我得到了同样的错误。 我试图静态连接,但你需要在构造函数中进行连接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM