Hot questions for Using Azure in android studio

Top Java Programmings / Azure / android studio

Question:

I'm trying to implement an Azure Mobile Service in an Android app, using the documentation from inside the Azure portal (Portal > Mobile Service > Android > Connect Existing Android App), and am running into the java malformed URL error when trying to instantiate a MobileServiceClient.

Code that is throwing the error:

mClient = new MobileServiceClient(
    "https://myurl.azure-mobile.net/",
    "my_secret_squirrel_key",
    this
);

The compiler is throwing this error:

Unhandled exception:java.net.MalformedURLException

I've added the buildscript and compile declarations in the project/app Gradle files. Using version 2.0.3 of the SDK (tried using 2.0.2 as well).

I've added permissions to the manifest file, too:

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

The activity I'm using this in is importing the mobile services library:

import com.microsoft.windowsazure.mobileservices.*;

And I'm declaring a private member in that same activity to hold the MobileServiceClient like so:

private MobileServiceClient mClient;

Using Android Studio 1.3.2.


Answer:

The URL constructor has a checked MalformedUrlException

You need to either add throws MalformedUrlException to your calling method, or wrap it in a try/catch:

try
{
    mClient = new MobileServiceClient(
        "https://myurl.azure-mobile.net/",
        "my_secret_squirrel_key",
        this);
}
catch(MalformedUrlException ex)
{
    Log.e("Service URL was malformed!", ex);
}

Question:

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)

Answer:

Per my experience, the exception "NoClassDefFoundError" means your project missing a dependency library jar file. At here, your project miss gson.jar file.

You can download a zip file "azuresdk-android-2.0.3.zip" from the section "Mobile" -> "Android install" of the page https://azure.microsoft.com/en-us/downloads/. After decompress the zip file to a directory, you can find the "gson-2.2.2.jar" file and other dependency jar files in the directory "azuresdk-android-2.0.3/mobileservices".

Then you can import these jar library files into directory "libs" of your project to resolve the issue. Please refer to the SO thread Android Studio - Importing external Library/Jar to learn how to import external library/jar.

Best Regards.