Reputation: 367
I need help to setup a direct connection to a MongoDB database (locally) using Android Studio on a Linux Mint 17.1 machine.
I made a new & clean project in Android Studio as you can see below.
After that I made new Java Class with the following code:
package com.example.bebo.connection;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import java.util.Set;
public class MongoPortal {
public boolean insert(){
try {
MongoClient mongoClient = new MongoClient( "localhost" , 27107 );
DB db = mongoClient.getDB("test");
// Get and print all the collections
Set<String> colls = db.getCollectionNames();
for (String s : colls)
System.out.println(s);
mongoClient.close();
}
catch (Exception e){
e.printStackTrace();
return false;
}
return true;
}
}
Last but not least, I changed the dependencies at build.gradle(Module: app) to:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'org.mongodb:mongo-java-driver:3.0.0'
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.android.support:support-v4:18.0.0'
}
Building.. Output:
I just need the easiest way to setup a connection, but I can't figure out what I am missing here. Everything is installed correctly as far as I know.
Upvotes: 0
Views: 4892
Reputation: 1287
This happened to me twice both the times the problem was with dependencies. This as far as I know this happens if you have same library with different versions (for me it was appcompat) or if you forget to list any dependency that needs to be listed.
Can you verify all jar files you have in lib folder (worst case by removing one by one) and also check dependencies once. Especially appcompat.
hope this helps.
Upvotes: 1