简体   繁体   中英

Retrofit different call and response json bodies

How can be done retrofit PUT with different types of call and responce bodies? I have a limitation with error. Types must be identical <T> and <T> . Responce bodie can contain int value, but call should not, because when i initialize CallBody object with int value, it already contain n1=0, and send this parameter to server.

@PUT("/api/test")
Call<CallBody> test1(@Body CallBody params_body);

public class CallBody {
    public String id;
}

public class ResponceBody {
    public String s1;
    public int n1;
}

public void Test(String id) {
        CallBody params_body = new CallBody();
        params_body.id = id;

        Call<CallBody> call1 = apiInterface.test1(params_body);
        call1.enqueue(new Callback<CallBody>() {
            @Override
            public void onResponse(Call<CallBody> call, Response<ResponceBody> response) {
                ResponceBody response = response.body();
            }

            @Override
            public void onFailure(Call<CallBody> call, Throwable t) {
                call.cancel();
            }
        });
    }

Try,

public class ResponceBody {
    public String s1;
    public Integer n1;
}

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