kumar kundan kumar kundan - 1 month ago 17
Android Question

Java - How to Retrieve and use a single value from azure mobile services in Android

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).