Trying to connect to shared mongodb using java

this is the code i’m using to connect from my android device to Atlas mongodb cluster…
it’s two week i’m trying…no result.
Please someone can try to help me to solve this problem ?

String uri = “mongodb+srv://”+ TestApp + “:” + Password + “@cluster0-shard-00-01.t36nd.mongodb.net/?authSource=admin”;

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    MongoClient client = new MongoClient(uri,27017);

   try {
        MongoDatabase database = client.getDatabase("TestApp");
        MongoCollection<Document> collection = database.getCollection("TestAppCollection");
        BsonDocument filter = new BsonDocument();
        collection.countDocuments(filter);
        client.close();
    } catch (MongoCommandException me) {
        return "An error occurred while attempting to run a command: " + me;
    } catch (MongoClientException mcexception) {
        return "MCERR# " + mcexception.getMessage();
    } catch (Exception exception) {
        return "ERR# " + exception.getMessage();
    }
    return "Connected";

the error i get it’s:

com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=mongodb+srv://xxxxxxxx:yyyyyyyy@cluster0-shard-00-01.t36nd.mongodb.net:27017/?authsource=admin:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: Unable to resolve host “mongodb+srv://testapp:ocmvix2gblfwljni@cluster0-shard-00-01.t36nd.mongodb.net:27017/?authsource=admin”: No address associated with hostname}, caused by {java.net.UnknownHostException: Unable to resolve host “mongodb+srv://testapp:ocmvix2gblfwljni@cluster0-shard-00-01.t36nd.mongodb.net:27017/?authsource=admin”: No address associated with hostname}}]

thanks in advance
Alberto.

1 Like

You can try this link. My one is solved by following the instruction in this link.