我在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
});
}
}
});
};
我是用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());
}
}
});
正在调用Custom API,但似乎没有传递参数 - request.body.members.id
不存在。
如何在Android上将参数正确传递到自定义API?
答案 0 :(得分:0)
您应该为此创建自定义类。例如,您想发送成员信息,您可以创建如下的类:
import com.google.gson.annotations.SerializedName;
public class Member {
@SerializedName("id")
public int ID;
@SerializedName("name")
public String Name;
}
现在调用invokeApi方法并传入Member对象。
Member member = new Member();
member.ID = 1;
member.Name = "Someone";
mClient.invokeApi("custAPI", "POST", member, Member.class, new ApiOperationCallback<custAPI>()
在您的自定义API中,您可以访问您的对象,您可以使用request.body.id作为id,使用request.body.name访问成员对象内的名称。