简体   繁体   English

Android中的Azure移动服务自定义API参数

[英]Azure Mobile Services Custom API Parameters in Android

I have a Custom API set up in Azure Mobile Services (JS): 我在Azure移动服务(JS)中设置了自定义API:

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: 我从Android用以下方式调用它:

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. 正在调用自定义API,但似乎未传递参数request.body.members.id不存在。

How do I properly pass parameters into a Custom API on Android? 如何在Android上将参数正确传递到自定义API?

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. 现在调用invokeApi方法并传入Member对象。

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. 在自定义API中,您将可以访问您的对象,并且可以使用request.body.id作为ID,并使用request.body.name作为成员对象中的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM