How to pass string in 'Body' Parameter of Retrofit 2 in android

放肆的年华 提交于 2019-12-18 13:00:34

问题


@POST("api/login")
Call<ApiResponse> loginUser(@Body String user);

Here the string is actually a JSONstring i.e.

{"email":"test@gmail.com","password":"test"}

Couldnt figure out what is wrong in this. Either the string it again converted to json. Please suggest..

This is what i want to do to my request as shown in picture.


回答1:


Convert your data in object

public class Credentials
{
    public String email;
    public String password;
}

Set the data to object

Credentials loginCredentials = new Credentials();
loginCredentials.email = "test@gmail.com";
loginCredentials.password = "password";

Call your api

@POST("api/login")
Call<ApiResponse> loginUser(@Body Credentials credentials);



回答2:


@POST("api/login")
Call<ApiResponse> loginUser(@Body HashMap<String, String> user);

We can use Hasmap here like this.



来源:https://stackoverflow.com/questions/35082844/how-to-pass-string-in-body-parameter-of-retrofit-2-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!