[英]java.net.UnknownHostException: Unable to resolve host "@MYIPADDRESS, when using android + mongodb
I have created a MongoClientURI like this: 我创建了一个像这样的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);
And I am calling this method from AsyncTask : 我从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;
}
I also have all permissions required for internet connection too: 我也拥有互联网连接所需的所有权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
But still I am not able to insert values to the database running in that IP address. 但是我仍然无法向在该IP地址中运行的数据库插入值。 The full error says : 完整错误说:
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)}}]
I know the IP ADDRESS is correct as I can type this in chrome http://MYIPADDRESS:27017/
我知道IP ADDRESS是正确的,因为我可以在chrome http://MYIPADDRESS:27017/
And got this in return: 得到这个回报:
It looks like you are trying to access MongoDB over HTTP on the native driver port.
FWIW I was getting the same error. FWIW我得到了同样的错误。 I was trying to connect statically, but you need to do it in the constructor. 我试图静态连接,但你需要在构造函数中进行连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.