2

This is how I add a Java driver to my project

compile 'org.mongodb:mongo-java-driver:2.13.2'

after I run my Android app Gradle crushes. if I add the jar directly, my app crushes anyway.

Connection code below:

MongoClient mongoClient = new MongoClient("mongodb://Dbuser:dbpass@ds047692.mongolab.com:47692");
DB db = mongoClient.getDB("testdb");
Set<String> collectionNames = db.getCollectionNames();

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:

To connect using a driver via the standard URI: mongodb://blablauser:password@ds047692.mongolab.com:47692/urdb

But in the code it doesn't connect.

I use mongo-java-driver. But every time i try to even read my db i get

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

Why i have no access to my own db?

TooLazy
  • 836
  • 4
  • 11
  • 29
  • By creating an API layer as a "service" for your application to connect to and work with data from the database layer. **Never** connect remove clients directly to a database. Lots of security issues, lots of performance optimization issues. Use an API. – Blakes Seven Jul 19 '15 at 11:44
  • possible duplicate of [Connecting MongoDB from Android app](http://stackoverflow.com/questions/21554887/connecting-mongodb-from-android-app) – Blakes Seven Jul 19 '15 at 11:45
  • Are you going to deploy that app to the public? Then this is a very, very bad idea. Better put an application server between the database and the internet to properly enforce what a user can and can't do with the database. – Philipp Jul 19 '15 at 12:02
  • I just need to connect to my db via url and mongo-java-driver – TooLazy Jul 19 '15 at 13:29
  • 2
    Besides the previous comments regarding security concerns (they are damn right!): Does your app has the permission to connect via Internet? – rzo1 Jul 25 '15 at 12:33
  • Did you put internet permission in your app's AndroidManifest.xml file? – CLOUGH Nov 15 '15 at 10:32

2 Answers2

4

Try using this

MongoClientURI mongoUri  = new MongoClientURI("mongodb://Dbuser:dbpass@ds047692.mongolab.com:47692");
MongoClient mongoClient = new MongoClient(mongoUri);
DB db = mongoClient.getDB("testdb");
Set<String> collectionNames = db.getCollectionNames();

First you need to convert the uri into MongoClientURI format.

Ravin Gupta
  • 783
  • 7
  • 13
-1

I don't know if you solved it but i had tha same problem and i used this http://docs.mlab.com/data-api/ with http requests.