Search code examples
javaandroidazureandroid-studioazure-mobile-services

Connecting Azure to Android Studios


I am trying to connect Azure to Android Studios. I have followed the tutorial on Azure and created a mobile service. It then gave me some code to add to my app so that they are connected. However, the code is throwing an exception which I cannot figure out. I have searched the internet from head to toe and cannot find an answer. I think it has something to do with the dependencies or the way the library was imported (which was by pasting a jar file into the libs folder and adding a dependency).

The code throwing causing the exception (provided by Azure):

try {
        mClient = new MobileServiceClient(
                "https://atm.azure-mobile.net/",
                "JWjetFMUVaAXzHmqDVqhkkRhTGGjeW70",
                this
        );
        Item item = new Item();
        item.Text = "Awesome item";
        mClient.getTable(Item.class).insert(item, new TableOperationCallback<Item>() {
            public void onCompleted(Item entity, Exception exception, ServiceFilterResponse response) {
                if (exception == null) {
                    // Insert succeeded
                    Toast.makeText(context, "WOOHOO", Toast.LENGTH_LONG).show();
                } else {
                    // Insert failed
                    Toast.makeText(context, "FAILED", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
    catch(MalformedURLException d) {
    }

The exception:

java.lang.NoClassDefFoundError: com.google.gson.GsonBuilder
        at com.microsoft.windowsazure.mobileservices.MobileServiceClient.createMobileServiceGsonBuilder(MobileServiceClient.java:192)
        at com.microsoft.windowsazure.mobileservices.MobileServiceClient.<init>(MobileServiceClient.java:179)
        at com.microsoft.windowsazure.mobileservices.MobileServiceClient.<init>(MobileServiceClient.java:158)
        at com.atmlocator.hooper.kenneth.atmlocator.HomeActivity.onCreate(HomeActivity.java:218)
        at android.app.Activity.performCreate(Activity.java:5047)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2051)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2112)
        at android.app.ActivityThread.access$700(ActivityThread.java:139)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1223)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4917)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:997)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
        at dalvik.system.NativeStart.main(Native Method)

Solution

  • I have solved the problem by updating the .jar file. Although the Microsoft documentation told me to get:

    'gson:2.2.2'
    

    This version does not work. Instead I downloaded a newer version:

    'gson:2.3.1'
    

    This .jar file solves the issue