Search code examples
androidazure-mobile-servicesazure-api-apps

How to Store Multiple records in My Array List from Azure mobile service Using API Code?


I created completeall2 API in Azure Mobile service..

My API Coding

exports.post = function(request, response) {
var mssql = request.service.mssql;
var sql = "SELECT * from productmovement";
mssql.query(sql, {
    success: function(results) {            

                   response.send(200, results);

    }    })  };

What's Code for store this results into my ArrayList<ProductMovement> in my app..


Solution

  • 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("productmovement",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));
            }
        }
    });