从Azure移动服务将数据检索到Java中的Android应用程序 [英] Retrieve data from a azure mobile service into a android apllication in java
问题描述
我有一个Azure移动服务,该服务从多个来源检索数据并将其提供给我的移动应用程序使用.到目前为止,我已经在开发Windows Mobile应用程序中使用了它,并且它工作得很好.现在,我想将同一个应用程序扩展到android,我想利用来自同一个Azure移动服务的数据.
I have a Azure mobile Service which retrieves data from multiple sources and delivers it for my mobile application to consume . 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应用程序以检索数据的C#代码是
My C# code for the Windows app to retrieve the data is
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应用程序的JAVA中使用它
I tried using this in JAVA for the android app
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#代码.
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 . can you please point of the mistake and help me out to implement the above c# code in the new syntax in Java.
构建代码时出现以下异常.
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)
我尝试使用
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
推荐答案
基于我的理解,您希望使用Java for Android从Mobile Service的JavaScript后端中的自定义API端点检索数据,例如C#中的代码.
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#.
假设您熟悉C#,即Java.我认为您可以尝试查看MobileServiceClient
类的javadoc,以了解如何使用具有不同参数的函数invokeApi
.我找不到此javadoc,但我认为您可以从
Assumption that you are familiar with C#, nto Java. I think you can try to review the javadoc of Class MobileServiceClient
to know how to use the functions invokeApi
with different arguments. 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.
同时,下面有一些官方教程和博客可以帮助您学习该原理.
Meanwhile, there are some offical tutorials and blog below that can help learning the principle.
- How to: define a custom API endpoint in a JavaScript backend mobile service
- The section
How to: Call a custom API
of the docHow to use the Android client library for Mobile Services
- The blog
Custom API in Azure Mobile Services – Client SDKs
from MSDN
希望它会有所帮助.最好的问候.
Hope it helps. Best Regards.
更新:
从后端响应内容,如下所示:
Response content from backend like as below:
{"name": "peter", "age": 28}
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);
这篇关于从Azure移动服务将数据检索到Java中的Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!