简体   繁体   English

Java - 如何从Android中的azure移动服务中检索和使用单个值

[英]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. 我是azure的新手,但我知道如何检索和存储数据到azure,我为此目的遵循azure官方文档。

Link is Here - https://azure.microsoft.com/en-in/documentation/articles/mobile-services-android-get-started-data/ 链接在这里 - 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 . 但问题是,本教程仅展示了如何使用适配器和列表从azure中检索和使用数据。 I want to know , How can i retrieve a single value from azure mobile services and how to use it in android . 我想知道,如何从azure移动服务中检索单个值以及如何在android中使用它

Plzz provide me both backend code (if there is any) and java code for this . Plzz为我提供了后端代码(如果有的话)和java代码。 THANKS in advance 提前致谢

I got it solved. 我解决了。 No need to create a custom API. 无需创建自定义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. 不要忘记创建一个用于serialization的类User 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. design[0]是一个大小为1的数组。

eq(x) is equal to x where , x variable contains username for which i want designation from database (azure). eq(x)等于x其中, x变量包含我想要从数据库(azure)指定的用户名。

You can do this with a custom API. 您可以使用自定义API执行此操作。 See this link: https://azure.microsoft.com/en-us/documentation/articles/mobile-services-how-to-use-server-scripts/#custom-api 请看以下链接: 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 . 然后可以访问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/ 然后,您可以按照Android客户端上的API使用说明进行操作: 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. 这会调用API。 You need to write the class and Future to handle the results. 您需要编写类和Future来处理结果。 The above page explains this in great detail. 上面的页面详细解释了这一点。

The most optimal solution would be to create an api on your server which accepts an ID to return an single object/tablerow. 最佳解决方案是在服务器上创建一个api,它接受一个I​​D来返回单个对象/ tablerow。

In your android app, you only have to call: 在你的Android应用程序中,你只需要调用:

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. YourClass应具有与服务器上的对象相同的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM