java – MongoDB does not work from Android Studio

Question:

There is a project in Android Studio. Added a section in the app module where the mongo driver for java is connected

dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'

compile 'org.mongodb:mongo-java-driver:3.2.2' }

Further in the project we have the following code

    MongoClient client = new MongoClient(new MongoClientURI("mongodb://velata:velata@ds025439.mlab.com:25439"));
    MongoDatabase base = client.getDatabase("velat");
    base.createCollection("NotMAC");
    client.close();

As a result, everything starts, but it crashes with such errors

05-01 16:16:01.444 2878-2878/com.example.myapplication W/org.bson.ObjectId: Failed to get machine identifier from network interface, using random number instead
                                                                        java.net.SocketException
                                                                            at java.net.NetworkInterface.rethrowAsSocketException(NetworkInterface.java:248)
                                                                            at java.net.NetworkInterface.collectIpv4Address(NetworkInterface.java:208)
                                                                            at java.net.NetworkInterface.getByNameInternal(NetworkInterface.java:127)
                                                                            at java.net.NetworkInterface.getNetworkInterfacesList(NetworkInterface.java:309)
                                                                            at java.net.NetworkInterface.getNetworkInterfaces(NetworkInterface.java:298)
                                                                            at org.bson.types.ObjectId.createMachineIdentifier(ObjectId.java:475)
                                                                            at org.bson.types.ObjectId.<clinit>(ObjectId.java:463)
                                                                            at com.mongodb.connection.ClusterId.<init>(ClusterId.java:47)
                                                                            at com.mongodb.connection.DefaultClusterFactory.create(DefaultClusterFactory.java:69)
                                                                            at com.mongodb.Mongo.createCluster(Mongo.java:682)
                                                                            at com.mongodb.Mongo.createCluster(Mongo.java:668)
                                                                            at com.mongodb.Mongo.createCluster(Mongo.java:642)
                                                                            at com.mongodb.Mongo.<init>(Mongo.java:289)
                                                                            at com.mongodb.MongoClient.<init>(MongoClient.java:268)
                                                                            at com.example.myapplication.MainActivity.onCreate(MainActivity.java:21)
                                                                            at android.app.Activity.performCreate(Activity.java:5990)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                            at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:135)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:372)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                                         Caused by: android.system.ErrnoException: socket failed: EACCES (Permission denied)
                                                                            at libcore.io.Posix.socket(Native Method)
                                                                            at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:282)
                                                                            at java.net.NetworkInterface.collectIpv4Address(NetworkInterface.java:193)
                                                                            at java.net.NetworkInterface.getByNameInternal(NetworkInterface.java:127) 
                                                                            at java.net.NetworkInterface.getNetworkInterfacesList(NetworkInterface.java:309) 
                                                                            at java.net.NetworkInterface.getNetworkInterfaces(NetworkInterface.java:298) 
                                                                            at org.bson.types.ObjectId.createMachineIdentifier(ObjectId.java:475) 
                                                                            at org.bson.types.ObjectId.<clinit>(ObjectId.java:463) 
                                                                            at com.mongodb.connection.ClusterId.<init>(ClusterId.java:47) 
                                                                            at com.mongodb.connection.DefaultClusterFactory.create(DefaultClusterFactory.java:69) 
                                                                            at com.mongodb.Mongo.createCluster(Mongo.java:682) 
                                                                            at com.mongodb.Mongo.createCluster(Mongo.java:668) 
                                                                            at com.mongodb.Mongo.createCluster(Mongo.java:642) 
                                                                            at com.mongodb.Mongo.<init>(Mongo.java:289) 
                                                                            at com.mongodb.MongoClient.<init>(MongoClient.java:268) 
                                                                            at com.example.myapplication.MainActivity.onCreate(MainActivity.java:21) 
                                                                            at android.app.Activity.performCreate(Activity.java:5990) 
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                            at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:135) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

05-01 16:16:01.453 2878-2878/com.example.myapplication I/cluster: Cluster created with settings {hosts=[ds025439.mlab.com:25439], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}

05-01 16:16:01.482 2878-2878/com.example.myapplication I/cluster: No server chosen by WritableServerSelector from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, all=[ServerDescription{address=ds025439.mlab.com:25439, 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)}}]}. Waiting for 30000 ms before timing out

05-01 16:16:31.483 2878-2878/com.example.myapplication D/AndroidRuntime: Shutting down VM

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

What could be the problem? Maybe you need to customize something in Android Studio itself?

Answer:

Try with permissions like this

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

And do not forget to work with the database in a separate thread, otherwise the system will also swear to work with the network from the main thread.

Scroll to Top