2015-07-09 30 views
0

我有一個自定義的API在Azure的移動服務設置(JS):Azure的移動服務自定義API參數在Android的

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 
     }); 
} 
     } 
    }); 
}; 

我與調用它從安卓

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()); 
        } 
       } 
      }); 

的自定義API正在被調用,但參數似乎沒有被傳遞 - request.body.members.id不存在。

如何正確地將參數傳遞到Android上的自定義API?

回答

0

您應該爲此創建一個自定義類。例如,您想發送會員信息,您可以創建類如下:

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

現在調用invokeApi方法並傳遞成員對象。

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的成員對象的內部名稱。

+0

嘿...感謝您與我們分享...我有一個問題你能幫助....雖然我知道天藍色的各種東西像檢索和插入數據到非SQL表,但我仍然是新蔚藍,想要一些關於如何編寫和使用API​​的教程.plss提供給我一些鏈接 –

+0

還有一個問題...我如何檢索和使用android中的單個值。我正在採取單一的價值,而不是像azure教程中指定的整個列表。 –

相關問題