如何在Android中的Retrofit 2的'Body'参数中传递字符串 [英] How to pass string in 'Body' Parameter of Retrofit 2 in android

查看:2486
本文介绍了如何在Android中的Retrofit 2的'Body'参数中传递字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

此处的字符串实际上是JSONstring,即

Here the string is actually a JSONstring i.e.

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

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

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.

推荐答案

在对象中转换数据

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

将数据设置为对象

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

调用您的api

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

这篇关于如何在Android中的Retrofit 2的'Body'参数中传递字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆