简体   繁体   中英

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

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

Here the string is actually a JSONstring ie

{"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.

在此输入图像描述

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);
@POST("api/login")
Call<ApiResponse> loginUser(@Body HashMap<String, String> user);

We can use Hasmap here like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM