简体   繁体   English

从Azure移动服务将数据检索到Java中的Android应用程序

[英]Retrieve data from a azure mobile service into a android apllication in java

I have a Azure mobile Service which retrieves data from multiple sources and delivers it for my mobile application to consume . 我有一个Azure移动服务,可以从多个来源检索数据并将其提供给我的移动应用程序使用。 so far I have used it in developing windows mobile application and it works completely fine .Now that I want to extend the same app to android , I want to leverage the data from the same azure mobile service . 到目前为止,我已经在开发Windows Mobile应用程序中使用了它,并且它工作得很好。现在,我想将同一个应用程序扩展到android,我想利用来自同一个Azure移动服务的数据。

My C# code for the Windows app to retrieve the data is Windows应用程序用于检索数据的C#代码是

MobileServiceClient mobileservice = new MobileServiceClient("url", "key");
var aod_return = await mobileservice.InvokeApiAsync("CCOOutageHistoryData", HttpMethod.Get, null);
List<Data> aod_result = JsonConvert.DeserializeObject<List<Data>>(aod_return.ToString());
VList3.ItemsSource = aod_result;

I tried using this in JAVA for the android app 我尝试在Java应用程序的JAVA中使用它

try {
   mClient = new MobileServiceClient("url", "key", this);


   mClient.invokeApi("CCOOutageHistoryData",null, "GET", null, new ApiJsonOperationCallback() {
      @Override
      public void onCompleted(JsonElement jsonElement, Exception e, ServiceFilterResponse serviceFilterResponse) {
         GsonBuilder gsonb = new GsonBuilder();
         Gson gson = gsonb.create();

         JsonArray array = jsonElement.getAsJsonArray();
         List<MyObject> myObjects = new ArrayList<MyObject>();
            for(int i = 0; i < array.size(); i++) {
               myObjects.add(gson.fromJson(array.get(i).getAsJsonObject().toString(), MyObject.class));
            }
       }
   });
} catch (MalformedURLException e) {
   // Do nothing
}

However ,when I give the breakpoint , after initializing the mclient it doesnot enter to the next lines of code , and also the syntax I used for Mclient.Incokeapi is said to be deprecated . 但是,当我给出断点时,在初始化mclient之后,它不会输入到下一行代码,也不会使用我用于Mclient.Incokeapi的语法。 can you please point of the mistake and help me out to implement the above c# code in the new syntax in Java. 您能否指出错误并帮助我以Java的新语法实现上述c#代码。

I get the below exception when I build the code. 构建代码时出现以下异常。

invoke is not implemented
java.lang.UnsupportedOperationException: invoke is not implemented
    at com.jetbrains.cidr.lang.refactoring.introduce.OCBaseIntroduceHandler.invoke(OCBaseIntroduceHandler.java:263)
    at com.intellij.refactoring.actions.BaseRefactoringAction.actionPerformed(BaseRefactoringAction.java:125)
    at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher$3.performAction(IdeKeyEventDispatcher.java:593)
    at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.processAction(IdeKeyEventDispatcher.java:644)
    at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.inInitState(IdeKeyEventDispatcher.java:483)
    at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.dispatchKeyEvent(IdeKeyEventDispatcher.java:213)
    at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:538)
    at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:382)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

I tried using 我尝试使用

mClient.invokeApi("CCOOutageHistoryData",null, "GET", null, new ApiJsonOperationCallback() {
                @Override
                public void onCompleted(JsonElement jsonElement, Exception e, ServiceFilterResponse serviceFilterResponse) {
                    GsonBuilder gsonb = new GsonBuilder();
                    Gson gson = gsonb.create();                    JsonArray array = jsonElement.getAsJsonArray();
                    List<MyObject> myObjects = new ArrayList<MyObject>();
                    for(int i = 0; i < array.size(); i++)
                    {
                        myObjects.add(gson.fromJson(array.get(i).getAsJsonObject().toString(), MyObject.class));
                    }
                }
            });

but does not work . 但不起作用。 can some one please help me fix this issue 有人可以帮我解决这个问题吗

Based on my understanding, you want to using Java for Android to retrieve data from a custom API endpoint in JavaScript backend of Mobile Service, like your code in C#. 根据我的理解,您想使用Java for Android从Mobile Service的JavaScript后端中的自定义API端点检索数据,例如C#中的代码。

Assumption that you are familiar with C#, nto Java. 假设您熟悉C#,即Java。 I think you can try to review the javadoc of Class MobileServiceClient to know how to use the functions invokeApi with different arguments. 我认为您可以尝试查看Class MobileServiceClient的javadoc,以了解如何使用带有不同参数的invokeApi函数。 I can't find this javadoc, but I think you can get the similar helps from the comments of the source code of Class MobileServiceClient at https://github.com/Azure/azure-mobile-services/blob/master/sdk/android/src/sdk/src/main/java/com/microsoft/windowsazure/mobileservices/MobileServiceClient.java . 我找不到此javadoc,但我认为您可以从https://github.com/Azure/azure-mobile-services/blob/master/sdk/上 MobileServiceClient类的源代码注释中获得类似的帮助。 android / src / sdk / src / main / java / com / microsoft / windowsazure / mobileservices / MobileServiceClient.java

Meanwhile, there are some offical tutorials and blog below that can help learning the principle. 同时,下面有一些官方教程和博客可以帮助您学习该原理。

  1. How to: define a custom API endpoint in a JavaScript backend mobile service 如何:在JavaScript后端移动服务中定义自定义API端点
  2. The section How to: Call a custom API of the doc How to use the Android client library for Mobile Services How to: Call a custom API文档How to: Call a custom API部分How to: Call a custom API How to use the Android client library for Mobile Services
  3. The blog Custom API in Azure Mobile Services – Client SDKs from MSDN Custom API in Azure Mobile Services – Client SDKs的博客Custom API in Azure Mobile Services – Client SDKs来自MSDN的Custom API in Azure Mobile Services – Client SDKs

Hope it helps. 希望能帮助到你。 Best Regards. 最好的祝福。


Update: 更新:

Response content from backend like as below: 来自后端的响应内容如下:

{"name": "peter", "age": 28}

The Java code as below: Java代码如下:

public class Person {
   private String name;
   private int age;

   ....Getting & Setting Methods
}

// The code `Person.class` as the Class<E> clazz argument for the function invokeApi, not null
mClient.invokeApi("<controllerName>", "GET", Person.class);

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

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