title: How to connect to a MongoDB database, in Android?
body: <p>This is how I add a Java driver to my project</p>
  
  <pre><code>compile 'org.mongodb:mongo-java-driver:2.13.2'
  </code></pre>
  
  <p>after I run my Android app Gradle crushes.
  if I add the jar directly, my app crushes anyway.</p>
  
  <p>Connection code below:</p>
  
  <pre><code>MongoClient mongoClient = new MongoClient("mongodb://Dbuser:dbpass@ds047692.mongolab.com:47692");
  DB db = mongoClient.getDB("testdb");
  Set&lt;String&gt; collectionNames = db.getCollectionNames();
  </code></pre>
  
  <p>All I need is to connect from my app to the mongo database.
  I am not sure of the correctness of my connection string.
  In my db account I have found: </p>
  
  <p><strong>To connect using a driver via the standard URI: mongodb://blablauser:password@ds047692.mongolab.com:47692/urdb</strong></p>
  
  <p>But in the code it doesn't connect.</p>
  
  <p>I use mongo-java-driver. But every time i try to even read my db i get</p>
  
  <pre><code>java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.artem.mongodb/com.example.artem.mongodb.MainActivity}: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=ds047692.mongolab.com:47692, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketException: socket failed: EACCES (Permission denied)}, caused by {android.system.ErrnoException: socket failed: EACCES (Permission denied)}}]
  
  Document myDoc = collection.find().first();//crash point
  </code></pre>
  
  <p>Why i have no access to my own db?</p>