所有改裝post請求,格式化鋼軌API
我開始用戶RetroFit
首次,這是相當真棒。這就是說,我在格式化POST請求時遇到了一個障礙。
我使用指定的API,創建一個用戶,我需要發送的用戶對象是這樣的:
{
"user": {
"first_name": "John",
"last_name": "Doe",
"email": "[email protected]",
"password": "jigglypuff123",
"password_confirmation": "jigglypuff123"
}
}
我知道我能以這種形式發送JsonObject
,但我反而喜歡利用RetroFit
。
如果我傳遞User
對象,它不會被包裝在user
中。只是
{
"first_name": "John",
"last_name": "Doe",
"email": "[email protected]",
"password": "jigglypuff123",
"password_confirmation": "jigglypuff123"
}
發送。
我嘗試使用@Field
註釋,並結束了與此:
@POST("/users")
void createUser(@Field("user[first_name]") String first, @Field("user[last_name]") String last, @Field("user[email]") String email, @Field("user[password]") String password, @Field("user[password_confirmation]") String password_confirmation, Callback<User> cb);
我結束了這個錯誤:
@Field parameters can only be used with form encoding. (parameter #1)
有誰知道如何實現這一目標?
請參閱[此問題](https ://github.com/square/retrofit/issues/197)上的Retrofit Github回購。 – nhaarman 2015-02-23 22:49:48