0
votes

I have a Custom API set up in Azure Mobile Services (JS):

exports.post = function(request, response) {
    var tables = request.service.tables;
    var accounts = tables.getTable('Account');
    var item = {
        id: request.body.members.id
    }

    accounts.where(function (item) {
        return this.id == item.id;
    }, item).read({
    success: function (results) {
        if (results.length === 0) {
                response.send(200, { Status: "FAIL", Error: "Something went wrong." });
         }
         else
         {
        var account = results[0];
        response.send(200, {
           id: account.id,
           name: account.name,
           email: account.email
        });
}
        }
    });
};

I'm calling it from Android with:

List<Pair<String, String> > lp = new ArrayList<Pair<String, String> >();
            lp.add(new Pair("id", userId));

            mClient.invokeApi("custAPI", "POST", lp, whoami.class, new ApiOperationCallback<custAPI>() {

                @Override
                public void onCompleted(custAPI result,
                                        Exception error, ServiceFilterResponse response) {
                    if (error == null) {
                        Log.w("TEST", result.name.toString());
                    }
                }
            });

The Custom API is being called but the parameters do not seem to be being passed - request.body.members.id does not exist.

How do I properly pass parameters into a Custom API on Android?

1

1 Answers

0
votes

You should create a custom class for this. For example you want to send member info you can create a class like following:

import com.google.gson.annotations.SerializedName;
public class Member {
    @SerializedName("id")
    public int ID;
    @SerializedName("name")
    public String Name;
}

Now call the invokeApi Method and pass in Member object.

Member member = new Member();
member.ID = 1;
member.Name = "Someone";
mClient.invokeApi("custAPI", "POST", member, Member.class, new ApiOperationCallback<custAPI>()

And inside your custom API you will have access to your object and you can access using request.body.id for the id and request.body.name for the name inside the member object.