使用Retrofit 2.0在请求主体中发送JSON的Post请求。

huangapple go评论75阅读模式
英文:

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 :

{&quot;v1&quot; : &quot;username&quot;, &quot;v2&quot; : &quot;password&quot;}

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 :

{&quot;username&quot; : &quot;password&quot;}

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(&quot;200&quot;)
    private String resp;
    private String result;

    public String getResp() {
        return resp;
    }

    public String getResult() {
        return result;
    }
}

POST Interface

public interface JsonPlaceHolderApi {
    @POST(&quot;ratec&quot;)
    Call&lt;Post&gt; createPost(@Body Post post);
}

MainActivity Class

private void createPost() {
        final Post post = new Post(&quot;anthony&quot;, &quot;21.000008&quot;, &quot;72&quot;, &quot;2&quot;);
        Call&lt;Post&gt; call = jsonPlaceHolderApi.createPost(post);

        call.enqueue(new Callback&lt;Post&gt;() {
            @Override
            public void onResponse(Call&lt;Post&gt; call, Response&lt;Post&gt; response) {
                if (!response.isSuccessful()) {
                    textViewResult.setText(&quot;Code: &quot; + response.code());
                    return;
                }    

                Post postResponse = response.body();
                String content = &quot;&quot;;
                content += &quot;Code : &quot; + response.code() + &quot;\n&quot;;
                textViewResult.setText(content);

            }
            @Override
            public void onFailure(Call&lt;Post&gt; call, Throwable t) {
                textViewResult.setText(t.getMessage());
            }
        });
    }

As you can see, this is the parameter that I am sending :

final Post post = new Post(&quot;name&quot;, &quot;password&quot;);
    Call&lt;Post&gt; call = jsonPlaceHolderApi.createPost(post);

And in the POJO class, I have declared "v1" and "v2", so instead of sending this :

{&quot;username&quot; : &quot;password&quot;}

I am sending this :

{&quot;v1&quot; : &quot;username&quot;, &quot;v2&quot; : &quot;password&quot;}

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(&quot;ratec&quot;)
    Call&lt;Post&gt; createPost(@Body Map&lt;String,String&gt; 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>



huangapple
  • 本文由 发表于 2020年9月30日 09:32:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64129721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定