如何在android中的Retrofit 2的'Body'参数中传递字符串

时间:2022-07-20 16:37:15
@POST("api/login")
Call<ApiResponse> loginUser(@Body String user);

Here the string is actually a JSONstring i.e.

这里的字符串实际上是一个JSONstring,即

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

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

无法弄清楚这有什么问题。它再次转换为json的字符串。请建议..

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

这就是我想对我的要求做的,如图所示。

如何在android中的Retrofit 2的'Body'参数中传递字符串

2 个解决方案

#1


18  

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

打电话给你的api

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

#2


9  

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

We can use Hasmap here like this.

我们可以像这样在这里使用Hasmap。

#1


18  

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

打电话给你的api

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

#2


9  

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

We can use Hasmap here like this.

我们可以像这样在这里使用Hasmap。