Pages

Retrieving multiple values from a custom API on Azure Mobile Services with Android

I might well be missing something, but I had this problem as well:

http://stackoverflow.com/questions/23472205/android-and-azure-mobile-services-using-invokeapi-to-return-recordset

Essentially, the Android custom API for Android seems to only want to handle a single value, not at list.

My solution was to use the JSON version of the call, rather than the typed on, and then parse the JSON to get values, like this:

JsonObject request = new JsonObject();
request.addProperty("propertyname", value);

mClient.invokeApi("myapi", request, new ApiJsonOperationCallback() {
   
    @Override
    public void onCompleted(JsonElement result, Exception error,
            ServiceFilterResponse response) {
   
    doneWorking();
   
    JSONArray myResults;
        try {
        myResults = new JSONArray(result.toString());
       
        processResult(myResults);
       
} catch (JSONException e) {
// TODO Auto-generated catch block
Log.v("MyApp", "Failed");
}
   
    }
});

My object here has two items, voteoption and votes, so I get them like this. Processresults() takes the JSON array and processes it like this:

public void processResult(JSONArray myResults){
String voteoption;
String votes;
for(int n = 0; n < myResults.length(); n++)
        {
try{
JSONObject res = myResults.getJSONObject(n);
            
                             voteoption = res.getString("voteoption");
                             votes = res.getString("votes");

            
                                // do something with vote option and votes
}
catch  (JSONException e)
{
Log.v("MyApp", "Error sorting out the array " );
}

        }

}

I'm probably missing something, as the iPhone version of the API call works as expected with multiple values.

1 comment:

  1. A bittersweet ending to a day of scouring the web for a solution to this problem, but thanks very much for sharing your fix!

    ReplyDelete