Hot questions for Using Azure in azure mobile services

Top Java Programmings / Azure / azure mobile services

Question:

I am new to azure but i know certain things like how to retrieve and store data to azure , i followed azure official documentation for this purpose.

Link is Here - https://azure.microsoft.com/en-in/documentation/articles/mobile-services-android-get-started-data/

But the problem is, this tutorial is only showing How to retrieve and use Data from azure using Adapters and Lists . I want to know , How can i retrieve a single value from azure mobile services and how to use it in android.

Plzz provide me both backend code (if there is any) and java code for this . THANKS in advance


Answer:

I got it solved. No need to create a custom API.

Just follow the basics , Here is the code :-

final String[] design = new String[1];

private MobileServiceTable<User> mUser;

mUser = mClient.getTable(User.class);

            new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... params) {
                    try {
                        final MobileServiceList<User> result =
                                mUser.where().field("name").eq(x).execute().get();
                        for (User item : result) {
                           // Log.i(TAG, "Read object with ID " + item.id);
                            desig[0] = item.getDesignation(); //getDesignation() is a function in User class ie- they are getters and setters
                            Log.v("FINALLY DESIGNATION IS", desig[0]);

                        }

                    } catch (Exception exception) {
                       exception.printStackTrace();
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(Void aVoid) {
                    super.onPostExecute(aVoid);
                    designation.setText(desig[0]);
                }
            }.execute();

DON'T forget to create a class User for serialization and all. Also you should define the array .

FEEL FREE to write if you got it not working.

EDIT :-

design[0] is an array with size 1.

eq(x) is equal to x where , x variable contains username for which i want designation from database (azure).

Question:

I have these lines of code , which i want to use inside getview() method of CustomAdapter .

 new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... params) {
                    try {
                        final MobileServiceList<User> result =
                                mUser.where().field("name").eq(currentItem.getTo()).select("designation").execute().get();
                        int counter=
                                mUser.where().select("designation").execute().get().getTotalCount();
                        for (User item : result) {
                            // Log.i(TAG, "Read object with ID " + item.id);
                            desig[0] = item.getDesignation();
                            Log.v("FINALLY DESIGNATION IS", desig[0]);


                        }

                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(Void aVoid) {
                    super.onPostExecute(aVoid);
                    // gb.setDesignation(desig[0]);

                    designation.setText(desig[0]);

                }
            }.execute();

This is my AsyncTask code which , I am using to populate custom listview .This piece of code keeps on running , I don't know how many times. But at the end it is giving me right results after so many updates on TextView (designation). This is not only degrading the performance of my application but also showing multiple updates on TextView before reaching to result.

I am getting data in desig[0] variable , have a look on my code .

Data is coming from azure But don't worry if you are not an azure guy . Just help me on Java part.

QUESTION

My question is how can use Async task inside getview() method . Do i have to make functions , which i can call latter . Data is coming from azure But don't worry if you are not an azure guy . Just help me on Java part. plzz help


Answer:

getView gets called every time a line of list view must be drawn. So don't forget that there will be multiple doInBackground running at the same time. What is designation ? A view inside an element of ListView ?

EDIT

As designation is a text in each cell, you have to make sure that the cell you will upgrade is the good one. What I usually do is that I use a ViewHolder that I attach to the recycled View. Then I register the ViewHolder as a listener (listening to AsyncTask). ViewHolder starts the request and gives an ID to the AsyncTask. Then, while the AsyncTask is running, I periodically ask the ViewHolder if the ID that was given to the AsyncTask is the good one. If it's not, I cancel the AsyncTask (no need to be synchronous). Then when the AsyncTask is finished, I check a last time that the ID matches the one given by the ViewHolder and I notify the listener (ViewHolder) that the result is available. This way, you can be sure that the AsyncTask is not doing work for nothing.

But of course it will depend of what kind of task you are actually doing in AsyncTask...

Question:

I'm trying to implement azure offline sync in my android application (on Java). I found this example, but can't find any examples or documentation about using QueryOperations and Query. I'll be appreciated for any information.


Answer:

See the "How to: Query data from your Mobile App backend" section of the sibling page to the one you found, Work with the Client SDK. The Javadoc for the Mobile Apps Client SDK is also available, and of course there's also the Java source.

Question:

I am trying to send some data to Azure from my Android app. I am using the example provided by Azure when setting up a Mobile Service. I have no errors in the code but when I try and load an activity on a button click the app crashes. The activity I am trying to load has the Azure interaction code on the onCreate method.

UPDATE

This works on other android devices but when I try and run it on a Samsung Galaxy S3 Mini runnning Android version 4.1.2 it crashes.

Register.java

package com.example.martin.ivebeenthere;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;
import com.microsoft.windowsazure.mobileservices.MobileServiceClient;
import com.microsoft.windowsazure.mobileservices.http.ServiceFilterResponse;
import com.microsoft.windowsazure.mobileservices.table.TableOperationCallback;

import java.net.MalformedURLException;

public class Register extends AppCompatActivity {

    private MobileServiceClient mClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        try {
            mClient = new MobileServiceClient(
                    "AZURE ACCOUNT",
                    "AZURE KEY",
                    this
            );
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        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
                } else {
                    // Insert failed
                }
            }
        });
    }

    public void onClickbtnRegister(View view)
    {
        startActivity(new Intent(Register.this, Login.class));

        Toast.makeText(getApplicationContext(), "Successfully Registered",
                Toast.LENGTH_LONG).show();
    }

}

Item.java

package com.example.martin.ivebeenthere;

/**
 * Created by Martin on 31/03/2016.
 */
public class Item {
    public String Id;
    public String Text;
}

Logcat

04-06 10:28:31.570 15331-15331/com.example.martin.ivebeenthere E/AndroidRuntime: FATAL EXCEPTION: main 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.example.martin.ivebeenthere.Register.onCreate(Register.java:30)
at android.app.Activity.performCreate(Activity.java:5047)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
at android.app.ActivityThread.access$700(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
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:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)

Answer:

Do you have com.google.code.gson in your dependencies?

Look in dependencies list in build.gradle and ensure you have the following:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.code.gson:gson:2.3'
    compile 'com.google.guava:guava:18.0'
    compile 'com.squareup.okhttp:okhttp:2.5.0'
    compile 'com.microsoft.azure:azure-mobile-android:3.1.0'
    compile (group: 'com.microsoft.azure', name: 'azure-notifications-handler', version: '1.0.1', ext: 'jar')
}

Question:

I have decent background of azure mobile apps, but not on the android side.

and also complete java beginner.

Today, I am trying my hands at it, with azure mobile app running locally .

It has table controller with simple todotable with 1 column "Name"

and this is my code.

public class TodoItem {
   private String Id;
   private String Name;
}

private void CreateMobileAppClient() {

   String mobileAppUrl="http://localhost:58441/";
   try {
       mobileServiceClient = new MobileServiceClient(new URL(mobileAppUrl), this);
   } catch(IOException e){
       throw new RuntimeException(e);
   }
}

private void GetData() {

    MobileServiceTable<TodoItem>   mTable=mobileServiceClient.getTable(TodoItem.class);
    try {
        List<TodoItem> results = mTable.execute().get(100, TimeUnit.MILLISECONDS);
        Log.d("debug","data success");
        String str="";
    } catch(Exception e) {

    }
}

During debugging all is okay, till I get to execute().get() method.

At this point my app completely freezes, neither goes in catch block, nor goes to next line .

I don't see much in logs and messages either.

I think the question is similar to this one App not retrieving data from Azure mobile service table

Any help would be useful.

(P.S: table controller on the azure side, works perfect with fiddler, so no problems there.)


Answer:

Try to use:

MobileServiceList<TodoItem> results = mTable.execute().get(100, TimeUnit.MILLISECONDS);

Also you should get those results in an AsyncTask.

private void GetData()
    {

    MobileServiceTable<TodoItem> mTable = mobileServiceClient.getTable(TodoItem.class);
    MobileServiceList<TodoItem> results;

    new AsyncTask<Void, Void, Void>() {

          @Override
          protected Void doInBackground(Void... params) {
             try {
                 results = mTable.execute().get(100, TimeUnit.MILLISECONDS);
                 Log.d("debug","data success");
                 }
             catch(Exception e)
                 {

                 }
            return null;
           }
       }.execute();
    }

Question:

I am trying to upload video file of 10-11 MB on azure server.It working fine on 4g or wifi network.But it is throwing below error on 3g or 2g network.

java.io.IOException: The client could not finish the operation within specified maximum execution timeout. Please see the cause for further information..

Below is my code :--

try {
            // Setup the cloud storage account.
            CloudStorageAccount account = CloudStorageAccount
                    .parse("somestring");

            // Create a blob service client
            CloudBlobClient blobClient = account.createCloudBlobClient();
            BlobRequestOptions uploadOptions = new BlobRequestOptions();
            uploadOptions.setMaximumExecutionTimeInMs(300000);
            uploadOptions.setTimeoutIntervalInMs(100000);
            RetryPolicy retryPolicy=new RetryExponentialRetry(
                    60000 /* minBackoff in milliseconds */,
                    30000 /* delatBackoff in milliseconds */,
                    180000 /* maxBackoff in milliseconds */,
                    3 /* maxAttempts */);
            uploadOptions.setRetryPolicyFactory(retryPolicy);
            // Get a reference to a container
            // The container name must be lower case
            // Append a random UUID to the end of the container name so that
            // this sample can be run more than once in quick succession.
            CloudBlobContainer container = blobClient.getContainerReference("videos");

            // Create the container if it does not exist
            container.createIfNotExists();

            // Make the container public
            // Create a permissions object
            BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
            OperationContext opContext = new OperationContext();
            StorageEventMultiCaster storageEventMultiCaster = new StorageEventMultiCaster<>();

            // Include public access in the permissions object
            containerPermissions
                    .setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
            storageEventMultiCaster.addListener(new StorageEvent<SendingRequestEvent>() {

                @Override
                public void eventOccurred(SendingRequestEvent eventArg) {

                    // Do custom monitoring here...
                }
            });
            opContext.setSendingRequestEventHandler(storageEventMultiCaster);
            // Set the permissions on the container
            container.uploadPermissions(containerPermissions);
            CloudBlockBlob blob = container.getBlockBlobReference(file.getName());

            byte[] buffer = IOUtils.toByteArray(new FileInputStream(file));
            ByteArrayInputStream  inputStream = new ByteArrayInputStream(buffer);
            blob.upload(inputStream, inputStream.available(),null,uploadOptions,opContext);
            inputStream.close();
            String url = blob.getUri().toString();
                    } catch (Throwable t) {

        }

I am using com.microsoft.azure.android:azure-storage-android:2.0.0@aar library.

Any help would be appreciated.


Answer:

BlobRequestOptions has a property called singleBlobPutThresholdInBytes which determines whether or not a blob should be split in blocks while uploading. If you don't specify a value, the default value is 32 MB i.e. any file that is being uploaded, if its size is less than 32 MB, then it would be uploaded without splitting into blocks.

Since you mentioned that the file size is 10-11 MB which is smaller than this 32 MB limit, the SDK is trying to upload the file in one shot. Because of slow Internet connection, the request is timing out trying to upload this huge data.

What you could do is change it's value to a smaller size. The value should be more than 1 MB but less than 32 MB. Then your file will be uploaded in chunks and you should not get this timeout error (hopefully). To set the value, you can add following line of code:

uploadOptions.setSingleBlobPutThresholdInBytes(1024*1024);//1MB

Question:

I have got an Azure Mobile Android App with a SQL-Database and I want to create a new Easy Table everytime a certain action happens in my App. How do I do that? At the moment I am using the Mobile service table but it seems that there is no way to create a new table.


Answer:

Currently, there are no APIs to programmatically create tables.

Although not as simple, you can, however, create a custom API that will create the required table files in the appropriate table folder in the file system and, as long as dynamic schema is enabled, that would expose your new table as part of your Mobile Apps API.

Question:

I got some trouble using the asynctask to query in my cloud database.

Due the response delay to query I cant get the result correctly. Getting NULL.

MainActivity.java

   @Override
protected void onCreate(Bundle savedInstanceState) {
    this.mBox = new Box();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.novomenu_layout);
    InicializaAzure(); // init connection to azure mobile service


    this.mPalletDao = new PalletDAO(this);
    this.mBoxDao = new BoxDAO(this);

    mBox = mBoxDao.AzureGetBoxById(1); // query the cloud database
}

BoxDAO.java

  public Box AzureGetBoxById(final long id){
    final Box[] box = new Box[1];
    final boolean[] flag = {false};



        new AsyncTask<Void, Void, Void>() {


            private ProgressDialog pDialog;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(mContext);
                pDialog.setMessage("Just a moment...");
                pDialog.setIndeterminate(true);
                pDialog.setCancelable(true);
                pDialog.show();
            }

            @Override
            protected Void doInBackground(Void... params) {
                try {

                    final MobileServiceList<Box> result = mBoxTable.where().field("id").eq(id).execute().get();
                    Box mBox = result.get(0);
                    box[0] = mBox;

                } catch (Exception exception) {
                    //createAndShowDialog(exception, "Error");
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                pDialog.dismiss();
                flag[0] = true;
            }


        }.execute();




    return box[0];
    //return null;
}

I am getting always NULL until the asynctask has finished. but I need the result in the same time. How can I solve that? I've searched about asynctask but I didnt find anything like this.

Thank you.


Answer:

Your code is correct, and it works fine. However, if you want to get the result to show in the same time of UI displayed, you can not solve it easily by using the asynctask.

Per my experience, there are two ways can help solve that.

  1. Remove the asynctask code and use the sync method to get data, but it will cause UI hang so that it not be recommended.

  2. Use MobileServiceSyncTable to enable offline sync to solve it.

There is a sample doc https://azure.microsoft.com/en-us/documentation/articles/mobile-services-android-get-started-offline-data/ to help adding offline data sync into your app.

You alse can watch some vedio to learn it, please move to http://channel9.msdn.com/Shows/Cloud+Cover/Episode-155-Offline-Storage-with-Donna-Malayeri and http://azure.microsoft.com/documentation/videos/azure-mobile-services-offline-enabled-apps-with-donna-malayeri/.

Question:

Using Android and Java Script backend.

I am saving my user data to a table in Azure mobile services. I need to give "forgot password" functionality for users. Any one please suggest me a optimum solution for adding this functionality. Do I have to purchase an add-on or something?


Answer:

Per my experience, there are two ways to impliment forgot password function in Azure Mobile Service if user account information includes email account, phone number.

Solution 1: Email password reset link to User

You can use the "SendGrid" Service on Azure Marketplace to email a password reset link to user registed mailbox what selected in table by phone number or user name. User change the password thru the reset link page on PC browser.

About using "SendGrid" to send email, please refer to https://azure.microsoft.com/en-us/documentation/articles/store-sendgrid-mobile-services-send-email-scripts/.

Solution 2: Send a confirm code thru SMS

You can use the "Twilio" Service on Azure Marketplace to send a confirm code thru SMS in Android Client. User change the password on Android form UI after confirm the received code by sending back it to Mobile Service.

About using "Twilio" to send SMS, please refer to https://azure.microsoft.com/en-us/documentation/articles/partner-twilio-mobile-services-how-to-use-voice-sms/#howto_send_sms.

Best Regards.