2015-02-23 97 views
3

所有改裝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) 

有誰知道如何實現這一目標?

+0

請參閱[此問題](https ://github.com/square/retrofit/issues/197)上的Retrofit Github回購。 – nhaarman 2015-02-23 22:49:48

回答

4

試試這個:

@FormUrlEncoded 
@POST("/users") 
void createUser(@Field("user") User user, Callback<User> cb); 

讓我知道會發生什麼:)

0

我不知道你是否已經解決您的問題,但對於改造2這應該工作:

@POST("https://stackoverflow.com/users/") 
Call<User> createUser(
    @Query("user[first_name]") String firstName, 
    @Query("user[last_name]") String lastName, 
    @Query("user[email]") String email, 
    @Query("user[password]") String password, 
    @Query("user[password_confirmation]") String confirmation 
); 

因此,與您的代碼相比 - 唯一發生變化的是@Field註釋,應該用@Query註釋替換(當然還有更新爲改進2 adapta改變)。

最後 - 對於上述情況,請記住使用SSL協議來處理數據加密。

1

用這個接口

@FormUrlEncoded 
@POST("https://stackoverflow.com/users/create.json") 
public void insertUser(
     @Field("user[name]") String name, 
     @Field("user[email]") String email, 
     @Field("user[password]") String password, 
     Callback<Response> callback); 

並在機器人活動中使用該

private void insertUser(){ 
    //Here we will handle the http request to insert user to mysql db 
    //Creating a RestAdapter 
    RestAdapter adapter = new RestAdapter.Builder() 
      .setEndpoint(ROOT_URL) //Setting the Root URL 
      .build(); //Finally building the adapter 

    //Creating object for our interface 
    RegisterAPI api = adapter.create(RegisterAPI.class); 

    //Defining the method insertuser of our interface 
    api.insertUser(

      //Passing the values by getting it from editTexts 
      mname, 
      memail, 
      mpass, 

      //Creating an anonymous callback 
      new Callback<Response>() { 
       @Override 
       public void success(Response result, Response response) { 
        //On success we will read the server's output using bufferedreader 
        //Creating a bufferedreader object 
        BufferedReader reader = null; 

        //An string to store output from the server 
        String output = ""; 

        try { 
         //Initializing buffered reader 
         reader = new BufferedReader(new InputStreamReader(result.getBody().in())); 

         //Reading the output in the string 
         output = reader.readLine(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 

        //Displaying the output as a toast 
        Toast.makeText(register.this, output, Toast.LENGTH_LONG).show(); 
       } 

       @Override 
       public void failure(RetrofitError error) { 
        //If any error occured displaying the error as toast 
        Toast.makeText(register.this, error.toString(),Toast.LENGTH_LONG).show(); 
       } 
      } 
    ); 
} 

凡MNAME和memail是字符串,使用要傳遞和在軌使用 高清創建 @user = User.new(user_params)

respond_to do |format| 
    if @user.save 
    format.html { redirect_to @user, notice: 'User was successfully created.' } 
    #format.json { render :text, status: :created, location: @user} 
    format.json { render :status => 200, 
      :json => { :success => true, 
        :info => "Registered Successfully" }} 
    else 
    format.html { render :new } 
    format.json { render json: @user.errors, status: :unprocessable_entity } 
    end 
end 
end 

def user_params 
    params.require(:user).permit(:name, :email, :password) 
end