使用包含参数的json对象对POST进行改造

时间:2022-02-11 21:31:15

I'm using Retrofit to send a POST request to my server:

我正在使用Retrofit向我的服务器发送POST请求:

@POST("/login")
void login( @Body User user ,Callback<User> callback);

Where my user object has only email and password fields.

我的用户对象只有电子邮件和密码字段。

Checking the logs, I can see that my parameters are sent with this format:

检查日志,我可以看到我的参数是以这种格式发送的:

D/Retrofit﹕{"email":"example@test.com","password":"asdfasdf"}

What I need to do to my parameters be sent like this?

我需要对我的参数做什么才能这样发送?

{"user" :  {"email":"example@test.com","password":"asdfasdf"} }

2 个解决方案

#1


6  

EDIT: Making the right way, using a custom JsonSerializer:

编辑:使用自定义JsonSerializer以正确的方式:

public class CustomGsonAdapter {
    public static class UserAdapter implements JsonSerializer<User> {
        public JsonElement serialize(User user, Type typeOfSrc,
                                     JsonSerializationContext context) {
            Gson gson = new Gson();
            JsonElement je = gson.toJsonTree(user);
            JsonObject jo = new JsonObject();
            jo.add("user", je);
            return jo;
        }
    }
}

And then, on your API Client builder:

然后,在您的API客户端构建器上:

public static RestApiClient buildApiService() {
    Gson gson = new GsonBuilder()
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
            .registerTypeAdapter(User.class, new CustomGsonAdapter.UserAdapter())
            .create();

    RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(API_URL)
            .setConverter(new GsonConverter(gson))
            .build();
    return restAdapter.create(MudamosApi.class);
}

#2


1  

The simplest mode to solve your problem is creating a RequestPOJO class, for example:

解决问题的最简单模式是创建RequestPOJO类,例如:

File User.java:

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

File LoginRequestPojo.java:

public class LoginRequestPojo{
    public User user;

    public LoginRequestPojo(User user){
        this.user = user;
    }
}

And, in your retrofit 2 request:

并且,在您的改造2请求中:

@POST("/login")
void login( @Body LoginRequestPojo requestPojo, Callback<User> callback);

Finally, your request body:

最后,您的请求正文:

{"user":{"email":"someone@something.com","password":"123123"}}

#1


6  

EDIT: Making the right way, using a custom JsonSerializer:

编辑:使用自定义JsonSerializer以正确的方式:

public class CustomGsonAdapter {
    public static class UserAdapter implements JsonSerializer<User> {
        public JsonElement serialize(User user, Type typeOfSrc,
                                     JsonSerializationContext context) {
            Gson gson = new Gson();
            JsonElement je = gson.toJsonTree(user);
            JsonObject jo = new JsonObject();
            jo.add("user", je);
            return jo;
        }
    }
}

And then, on your API Client builder:

然后,在您的API客户端构建器上:

public static RestApiClient buildApiService() {
    Gson gson = new GsonBuilder()
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
            .registerTypeAdapter(User.class, new CustomGsonAdapter.UserAdapter())
            .create();

    RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(API_URL)
            .setConverter(new GsonConverter(gson))
            .build();
    return restAdapter.create(MudamosApi.class);
}

#2


1  

The simplest mode to solve your problem is creating a RequestPOJO class, for example:

解决问题的最简单模式是创建RequestPOJO类,例如:

File User.java:

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

File LoginRequestPojo.java:

public class LoginRequestPojo{
    public User user;

    public LoginRequestPojo(User user){
        this.user = user;
    }
}

And, in your retrofit 2 request:

并且,在您的改造2请求中:

@POST("/login")
void login( @Body LoginRequestPojo requestPojo, Callback<User> callback);

Finally, your request body:

最后,您的请求正文:

{"user":{"email":"someone@something.com","password":"123123"}}