Error with multiple @Body fields - retrofit2 beta3

I'm just starting with a modification for Android. I get an error when I try to specify 2 fields for the body of the mail request:

Several annotations of @Body method found. (parameter # 2) for the method

The call is defined in the API file as:

@POST("auth/login")
Call<UserData> login(@Body String username, @Body String password);

And I create a call with:

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseURL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

APIService service = retrofit.create(APIService.class);
Call<UserData> call = service.login(username, password);

an error occurs when making a call (he has no way to make it). When I delete one of the body fields, it seems to work fine.

Any ideas?

+4
source share
2 answers

Using multiple @Body is a bad idea, because @Body here means the body of the HTML POST message.

(: HTTP HTML-?)

, , , .

public class LoginInformation {
    String username;
    String password;
}

, .

@POST("auth/login")
Call<UserData> login(@Body LoginInformation loginInformation);
+8

HTTP- , @Body, "Multiple @Body method annotations found."

, :

HashMap , :

HashMap<String, Object> map = new HashMap<>();
map.put("password", "123456");
map.put("username", "Jake Warthon");

public class User(){
    private String username;
    private String password;

    public void setUsername(String username){
        this.username = username;
    }

    public void setPassword(String password){
        this.password = password;
    }
}

User user = new User(); 
user.setUsername("Jake Warthon")
user.setPassword("123456");
map.put("user", user);

(), (, )

map.put("user", user);
map.put("authorization", "12uh3u12huhcd2");
map.put("something", someobject);

Hashmap User

@POST("auth/login")
Call<UserData> login(@Body HashMap map);

@POST("auth/login")
Call<UserData> login(@Body User user);

, , .

Call<UserData> call = service.login(map); 

Call<UserData> call = service.login(user); 

, , .

+2

Source: https://habr.com/ru/post/1623362/


All Articles