0

Am trying to put a document on my MongoDB located in Azure. The problem I am facing is that I can't connect to the service. Azure provides the connector string so it should be working.

I am getting this errors

03-29 15:47:24.265 3578-3578/name.bagi.levente.pedometer W/org.bson.ObjectId: Failed to get process identifier from JMX, using random number instead                                                                      
        java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory;
    Caused by: java.lang.ClassNotFoundException: Didn't find class "java.lang.management.ManagementFactory"

and

FATAL EXCEPTION: main                                                                           Process: name.bagi.levente.pedometer, PID: 3578                                                                           java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.security.CodeSource java.security.ProtectionDomain.getCodeSource()' on a null object reference

I am using this code to connect

//Line below causes exception
MongoClient mongoClient = new MongoClient(new MongoClientURI("AzureProvidedString?ssl=true&sslInvalidHostNameAllowed=true")); 
        BasicDBObject document = new BasicDBObject();
        document.put("id",2);
        document.put("location", 3);
        document.put("steps", 4);
        mongoClient.getDatabase("FitnessData").getCollection("FitnessCollection").insertOne(Document.parse(document.toJson()));

My dependencies

dependencies{
        compile group: 'com.google.code.gson', name: 'gson', version: '2.8.0'
        // https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver
        compile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.4.2'

    }

I tried using Mongo driver 3.2.2 and am getting a different error. Other solutions here seem to be the same and are working.

David Makogon
  • 69,407
  • 21
  • 141
  • 189
  • Is this MongoDB in a VM? Or is it DocumentDB with MongoDB compatibility enabled? If the latter: Does the collection already exist? Or are you assuming your insert will implicitly create the collection? – David Makogon Mar 29 '17 at 19:47
  • Its MongoDb on actual Azure cloud. There is only one option for MongoDb on Azure. I run the code on physical phone as well. I created database and collection in the Azure Portal manually, the purpose of the device is to send data only. – pabekjunior Mar 29 '17 at 22:34

1 Answers1

0

It sounds like you want to access MongoDB on Azure from Android using Mongo Driver for Java. There was a similar SO thread mongodb 3.x driver Android compatibility which got a same issue with yours. The issue reason is the offical driver mongo-java-driver which is incompatible on Android. A solution is using the third-party driver porting for Android https://github.com/matfur92/mongo-java-driver.

However, directly access MongoDB on Android is not a good choice, even for sending data. Per my experience, the best practice is use REST webservice for sending & storing data on MongoDB, please refer to the MongoDB reference HTTP Interface to get fully featured RESTful Web Services powered by MongoDB on Azure WebApps, or just create Azure Mobile App to receive data from Android.

Community
  • 1
  • 1
Peter Pan
  • 23,476
  • 4
  • 25
  • 43