英文:
Post request JSON in body with Retrofit 2.0
问题
我想向一个API发送一个JSON参数,我已经实现了以下内容:
{"v1": "username", "v2": "password"}
所以基本上我正在发送包含"v1"和"v2"参数的2个JSON对象。但我想要实现的是像这样发送参数:
{"username": "password"}
我无法弄清楚如何做到这一点。以下是我现在的代码:
POJO类
class Post {
    
    private String v1;
    private String v2;
    private PostSuccess SUCCESS;
    
    public Post(String name, String password) {
        this.v1 = name;
        this.v2 = password;
    }
}
class PostSuccess {
    @SerializedName("200")
    private String resp;
    private String result;
    
    public String getResp() {
        return resp;
    }
    
    public String getResult() {
        return result;
    }
}
POST接口
public interface JsonPlaceHolderApi {
    @POST("ratec")
    Call<Post> createPost(@Body Post post);
}
MainActivity类
private void createPost() {
    final Post post = new Post("anthony", "21.000008", "72", "2");
    Call<Post> call = jsonPlaceHolderApi.createPost(post);
    call.enqueue(new Callback<Post>() {
        @Override
        public void onResponse(Call<Post> call, Response<Post> response) {
            if (!response.isSuccessful()) {
                textViewResult.setText("Code: " + response.code());
                return;
            }    
            Post postResponse = response.body();
            String content = "";
            content += "Code : " + response.code() + "\n";
            textViewResult.setText(content);
        }
        
        @Override
        public void onFailure(Call<Post> call, Throwable t) {
            textViewResult.setText(t.getMessage());
        }
    });
}
正如你所看到的,这是我发送的参数:
final Post post = new Post("name", "password");
Call<Post> call = jsonPlaceHolderApi.createPost(post);
在POJO类中,我声明了"v1"和"v2",所以我发送的是这样的:
{"v1": "name", "v2": "password"}
而不是这样的:
{"username": "password"}
我感谢你的帮助和建议。谢谢!
英文:
I want to send a JSON parameter to an API, and what I have achieved was like so :
{"v1" : "username", "v2" : "password"}
So basically I am sending 2 JSON object with "v1" and "v2" as the parameter. But what I wanted to achieve is sending the parameter like so :
{"username" : "password"}
I couldn't figure out how to do this. Here is my code for now :
POJO Class
class Post {
    private String v1;
    private String v2;
    private PostSuccess SUCCESS;
    public Post(String name, String password) {
        this.v1 = name;
        this.v2 = password;
    }
}
class PostSuccess {
    @SerializedName("200")
    private String resp;
    private String result;
    public String getResp() {
        return resp;
    }
    public String getResult() {
        return result;
    }
}
POST Interface
public interface JsonPlaceHolderApi {
    @POST("ratec")
    Call<Post> createPost(@Body Post post);
}
MainActivity Class
private void createPost() {
        final Post post = new Post("anthony", "21.000008", "72", "2");
        Call<Post> call = jsonPlaceHolderApi.createPost(post);
        call.enqueue(new Callback<Post>() {
            @Override
            public void onResponse(Call<Post> call, Response<Post> response) {
                if (!response.isSuccessful()) {
                    textViewResult.setText("Code: " + response.code());
                    return;
                }    
                Post postResponse = response.body();
                String content = "";
                content += "Code : " + response.code() + "\n";
                textViewResult.setText(content);
            }
            @Override
            public void onFailure(Call<Post> call, Throwable t) {
                textViewResult.setText(t.getMessage());
            }
        });
    }
As you can see, this is the parameter that I am sending :
final Post post = new Post("name", "password");
    Call<Post> call = jsonPlaceHolderApi.createPost(post);
And in the POJO class, I have declared "v1" and "v2", so instead of sending this :
{"username" : "password"}
I am sending this :
{"v1" : "username", "v2" : "password"}
I appreciate your help and suggestions. Thanks!
答案1
得分: 1
你可以直接在 @Body 中使用该映射,并按以下方式访问映射的键和值:
public interface JsonPlaceHolderApi {
    @POST("ratec")
    Call<Post> createPost(@Body Map<String, String> post);
}
英文:
You can directly use the map in the @Body and access the key and value of the map as below:
public interface JsonPlaceHolderApi {
    @POST("ratec")
    Call<Post> createPost(@Body Map<String,String> post);
}
答案2
得分: 0
class Post {
    @JsonProperty("username")
    private String v1;
    @JsonProperty("password")
    private String v2;
    private PostSuccess SUCCESS;
    public Post(String name, String password) {
        this.v1 = name;
        this.v2 = password;
    }
}
使用 JsonProperty 来自定义 JSON 变量,以适应你的需求。
<details>
<summary>英文:</summary>
class Post {
@JsonProperty("username")
private String v1;
@JsonProperty("password")
private String v2;
private PostSuccess SUCCESS;
public Post(String name, String password) {
    this.v1 = name;
    this.v2 = password;
}
}
use `JsonProperty` to customize the json variable to the way you need.
</details>
				通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论