我创建了一个像这样的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.
答案 0 :(得分:0)
FWIW我得到了同样的错误。我试图静态连接,但你需要在构造函数中进行连接。