![](/img/trans.png)
[英]java.net.UnknownHostException: Unable to resolve host No address associated with hostname
[英]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.