问题
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 .
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;
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
}
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)
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
回答1:
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#.
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 doc How 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.
Update:
Response content from backend like as below:
{"name": "peter", "age": 28}
The Java code as below:
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);
来源:https://stackoverflow.com/questions/35464458/retrieve-data-from-a-azure-mobile-service-into-a-android-apllication-in-java