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

前端 未结 3 427
情深已故
情深已故 2021-01-21 02:25

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

相关标签:
3条回答
  • 2021-01-21 02:48

    You can do this with a custom API. See this link: https://azure.microsoft.com/en-us/documentation/articles/mobile-services-how-to-use-server-scripts/#custom-api

    Code looks like this:

    exports.post = function(request, response) {
        response.send(200, "{ message: 'Hello, world!' }");
    } 
    

    It's then reachable at https://todolist.azure-mobile.net/api/APIFILENAME.

    If you want to access a table you can do something like:

    exports.post = function(request, response) {
        var userTable = tables.getTable('users');
    
        permissionsTable
            .where({ userId: user.userId})
            .read({ success: sendUser });
    } 
    
    function sendUser(results){
      if(results.length <= 0) {
        res.send(200, {});
      } else {
        res.send(200, {result: results[0]});
      }
    }
    

    You can then follow the instructions for using the API on your Android client here: https://azure.microsoft.com/en-us/documentation/articles/mobile-services-android-call-custom-api/

    How your app is written will change how this code works/looks, but it looks something like:

    ListenableFuture<MarkAllResult> result = mClient.invokeApi( "UsersAPI", MarkAllResult.class ); 
    

    That invokes the API. You need to write the class and Future to handle the results. The above page explains this in great detail.

    0 讨论(0)
  • 2021-01-21 02:48

    The most optimal solution would be to create an api on your server which accepts an ID to return an single object/tablerow.

    In your android app, you only have to call:

    MobileServiceTable<YourClass> mYourTable;
    
    mClient = new MobileServiceClient(
                            "https://yoursite.azurewebsites.net/",
                            mContext);
    mYourTable = mClient.getTable(YourClass.class);
    YourClass request = mYourTable.lookUp(someId).get(); 
    // request -> https://yoursite.azurewebsites.net/tables/yourclass/someId
    

    YourClass should have the same properties as the object on the server.

    0 讨论(0)
  • 2021-01-21 03:09

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

    0 讨论(0)
提交回复
热议问题