从azure移动服务中检索数据到java中的android apllication

时间:2016-02-17 18:11:32

标签: java c# android azure azure-mobile-services

我有一个Azure移动服务,它从多个来源检索数据并将其传递给我的移动应用程序以供使用。到目前为止,我已经用它来开发Windows移动应用程序,它完全正常。现在我想将同一个应用程序扩展到android,我想利用来自同一天蓝色移动服务的数据。

用于检索数据的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;

我尝试在JAVA中使用这个Android应用程序

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
}

但是,当我给出断点时,在初始化mclient之后它不会进入下一行代码,而且我用于Mclient.Incokeapi的语法也被认为是不推荐的。你能否指出这个错误,并帮助我在Java的新语法中实现上述c#代码。

我在构建代码时遇到以下异常。

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)

我尝试使用

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));
                    }
                }
            });

但不起作用。有人可以帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

根据我的理解,您希望使用Java for Android从Mobile Service的JavaScript后端中的自定义API端点检索数据,就像您在C#中的代码一样。

假设您熟悉C#,nto Java。我想您可以尝试查看类invokeApi的javadoc,以了解如何使用具有不同参数的函数MobileServiceClient。我无法找到这个javadoc,但我认为您可以从the python BigQuery API repo on Github{"name": "peter", "age": 28} 类的源代码的评论中获得类似的帮助。

同时,下面有一些官方教程和博客可以帮助学习这个原则。

  1. https://github.com/Azure/azure-mobile-services/blob/master/sdk/android/src/sdk/src/main/java/com/microsoft/windowsazure/mobileservices/MobileServiceClient.java
  2. How to: define a custom API endpoint in a JavaScript backend mobile service
  3. The section How to: Call a custom API of the doc How to use the Android client library for Mobile Services
  4. 希望它有所帮助。最诚挚的问候。

    更新

    来自后端的响应内容如下所示:

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

    Java代码如下:

    function(doc) {
      var shop, price, key;
      if (doc.item && doc.prices) {
        var cheapest = 99999999999;
        var cheapestshop = null;
        for (shop in doc.prices) {
          price = doc.prices[shop];
          if (price < cheapest) {
            cheapest = price;
            cheapestshop = shop;
          }
        }
        emit([doc.item, cheapest], cheapestshop);
      }
    }