Java – 如何从Android中的azure移动服务中检索和使用单个值
我是azure的新手,但我知道如何检索和存储数据到azure,我为此目的遵循azure官方文档。
链接在这里 – https://azure.microsoft.com/en-in/documentation/articles/mobile-services-android-get-started-data/
但问题是,本教程仅展示如何使用适配器和列表从azure检索和使用数据。 我想知道,如何从azure移动服务中检索单个值以及如何在android中使用它 。
Plzz为我提供了后端代码(如果有的话)和java代码。 提前致谢
我解决了。 无需创建自定义API。
只需遵循基础知识,以下是代码: –
final String[] design = new String[1]; private MobileServiceTable mUser; mUser = mClient.getTable(User.class); new AsyncTask() { @Override protected Void doInBackground(Void... params) { try { final MobileServiceList 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();
不要忘记创建一个用于serialization
的类User
。 您还应该定义数组。
如果你没有工作,请随意写 。
编辑: –
design[0]
是一个大小为1的数组。
eq(x)
等于x
其中, x
变量包含我想要从数据库(azure)指定的用户名。
您可以使用自定义API执行此操作。 请看以下链接: https : //azure.microsoft.com/en-us/documentation/articles/mobile-services-how-to-use-server-scripts/#custom-api
代码如下所示:
exports.post = function(request, response) { response.send(200, "{ message: 'Hello, world!' }"); }
然后可以访问https://todolist.azure-mobile.net/api/APIFILENAME
。
如果要访问表,可以执行以下操作:
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]}); } }
然后,您可以按照Android客户端上的API使用说明进行操作: https : //azure.microsoft.com/en-us/documentation/articles/mobile-services-android-call-custom-api/
您的应用程序的编写方式将改变此代码的工作方式/外观,但它看起来像:
ListenableFuture result = mClient.invokeApi( "UsersAPI", MarkAllResult.class );
这会调用API。 您需要编写类和Future来处理结果。 上面的页面详细解释了这一点。
最佳解决方案是在服务器上创建一个api,它接受一个ID来返回单个对象/ tablerow。
在你的Android应用程序中,你只需要调用:
MobileServiceTable 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
应具有与服务器上的对象相同的属性。