Connecting Android Studio to Cosmos Database

czyuan123 1 Reputation point
2021-03-03T16:47:34.067+00:00

I am working on to retrieve data from azure cosmos database to display in my android mobile app. However, I keep on having error with the client initialization. Below are my main codes:

----------

private static final String TAG = "";
private final String databaseName = "smartwristbanddb";
private final String containerName = "records";

public CosmosAsyncDatabase database;  
public CosmosAsyncContainer container;  

@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  

    System.out.println("Using Azure Cosmos DB endpoint: " + dbAccount.HOST);  

    //  Setting the preferred location to Cosmos DB Account region  
    //  West US is just an example. User should set preferred location to the Cosmos DB region closest to the application  
    //defaultPolicy.setPreferredLocations(Collections.singletonList("West US"));  
    //  <CreateSyncClient>  

    CosmosAsyncClient client;  
    client = new CosmosClientBuilder()  
            .endpoint(dbAccount.HOST)  
            .key(dbAccount.MASTER_KEY)  
            .consistencyLevel(ConsistencyLevel.EVENTUAL)  
            .contentResponseOnWriteEnabled(true)  
            .buildAsyncClient();  

    if(client != null) {  
        database = client.getDatabase(databaseName);  
        container = database.getContainer(containerName);  
    }else{  
        Log.e(TAG,"client error");  
    }  

}

----------

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,496 questions
{count} votes

1 answer

Sort by: Most helpful
  1. czyuan123 1 Reputation point
    2021-03-04T08:49:43.953+00:00

    Hi, I checked my connection key and endpoint, I copy and paste exactly from my account. I regenerated my primary key as well still showing error. The error starts from below:

    `W/m.example.myapp: type=1400 audit(0.0:9885): avc: denied { read } for name="somaxconn" dev="proc" ino=4276772 scontext=u:r:untrusted_app:s0:c28,c257,c512,c768 tcontext=u:object_r:proc_net:s0 tclass=file permissive=0


    W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.azure.cosmos.implementation.DatabaseAccount.getEnableMultipleWriteLocations()' on a null object reference
    at com.azure.cosmos.BridgeInternal.isEnableMultipleWriteLocations(BridgeInternal.java:163)
    W/System.err: at com.azure.cosmos.implementation.RxDocumentClientImpl.initializeGatewayConfigurationReader(RxDocumentClientImpl.java:264)
    at com.azure.cosmos.implementation.RxDocumentClientImpl.init(RxDocumentClientImpl.java:281)
    at com.azure.cosmos.implementation.AsyncDocumentClient$Builder.build(AsyncDocumentClient.java:203)
    W/System.err: at com.azure.cosmos.CosmosAsyncClient.<init>(CosmosAsyncClient.java:79)
    at com.azure.cosmos.CosmosClientBuilder.buildAsyncClient(CosmosClientBuilder.java:649)
    at com.azure.cosmos.CosmosClient.<init>(CosmosClient.java:30)
    at com.azure.cosmos.CosmosClientBuilder.buildClient(CosmosClientBuilder.java:661)
    W/System.err: at com.example.myapp.MainActivity.onCreate(MainActivity.java:52)
    at android.app.Activity.performCreate(Activity.java:7972)
    at android.app.Activity.performCreate(Activity.java:7961)
    W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3496)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3680)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
    W/System.err: at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2230)
    W/System.err: at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:227)
    at android.app.ActivityThread.main(ActivityThread.java:7802)
    at java.lang.reflect.Method.invoke(Native Method)
    W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1027)
    `

    Also for your information, this line shows in my logcat, when I clicked in the url to my cosmos db account:
    I/System.out: Using Azure Cosmos DB endpoint: https://smartwristbanddb.documents.azure.com:443/
    It shows:

    {"code":"Unauthorized","message":"Required Header authorization is missing. Ensure a valid Authorization token is passed.\r\nActivityId: d1a7bbec-1e8f-42cd-ad3e-d90177ea8190, Microsoft.Azure.Documents.Common/2.11.0"}

    the version of azure-cosmos library that I'm using:

    implementation 'com.azure:azure-cosmos:4.0.1-beta.4'