Android and Azure Mobile Services: Using invokeAPI

2020-06-23 13:42发布

问题:

I am trying something very simple. I have a custom API called "missingvehiclesfrominventoryjob" and it simply returns a record set from an standard SQL Query.

I can do this in my WinForms and Windows Phone app easily but I cannot figure out how to do this on the Android App.

Here is my code: (which DOES NOT COMPILE in Android Studio):

msClient.invokeApi("missingvehiclesfrominventoryjob", kd, new     
ApiOperationCallback<List<InventoryProspects>>(){
            @Override
            public void onCompleted(List<InventoryProspects> missingVehicles, Exception e,  
                   ServiceFilterResponse serviceFilterResponse){
                     for (InventoryProspects item : missingVehicles){
                        mAdapter.add(item);
                     }
               }
        });

The problem is the List in the parameters of the Callback. I am not sure how to indicate that the invoiceAPI call will return multiple rows from the database and I cannot find anywhere in the docs to explain how. Nor can I find an example ANYWHERE on the internet.

I am sure I am not the only on trying to do this.

Thanks in advance

Chuck Giddens

回答1:

What i did to overcome this problem, is to call a different overload of invokeApi that returns a JsonElement, and then deserialise it into my objects like so:

mClient.invokeApi("MyCustomApi",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));
            }
        }
    });


回答2:

I haven't had a chance to test it yet (will try when I have time and edit answer as needed) but my thinking is that the Android SDK won't allow you to do what you're trying to do. The invokeApi methods expect a strongly typed class to be set as the response type (or you can use the raw JSON methods). In this case, you're trying to say you want a list of items back, but I don't think that will work. I think you'll instead need to create a new class (i.e. missingvehiclesfrominventoryjobResponse) which contains a property that is of type List< InventoryProspects>. Note that you'll need to change your method call to actually match one of the available options for invokeApi which I don't believe it's doing right now. You can read more about the different formats of the method here: http://blogs.msdn.com/b/carlosfigueira/archive/2013/06/19/custom-api-in-azure-mobile-services-client-sdks.aspx

Alternatively, you can use the table methods against a table endpoint where the read expects a collection of results back.



回答3:

Have you tried to remote debug your API call from the app.[http://blogs.msdn.com/b/azuremobile/archive/2014/03/14/debugging-net-backend-in-visual-studio.aspx]. Your app will timed out in doing that but you can see line by line execution of your controller action if it returns the correct result set. If there is no problem with it then the problem should be in parsing result set.

What is the exception you are getting in callback? And have you tried using other method parameters such as passing with different HTTP methods? Use this as a reference as well. http://azure.microsoft.com/en-us/documentation/articles/mobile-services-android-get-started/

Please paste your exception or either controller action, and the object structure of the data transfer object of the result set.