预期是一个字符串,但在第3行第4列处是BEGIN_OBJECT,路径为$.SUCCESS。

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

Expected a string but was BEGIN_OBJECT at line 3 column 4 path $.SUCCESS

问题

我使用 Retrofit2 创建了一个 POST 请求方法,但在响应中遇到了以下问题:

    预期得到一个字符串,但在第3行第4列处实际得到了一个 BEGIN_OBJECT,路径为 $.SUCCESS

响应应该是:

    {
    "SUCCESS" :
    {
    "200" : "访问已授权",
    "ra" : "已批准",
    "la" : "已批准",
    "ch" : "已批准"
    }
    }

我使用了以下代码进行 POST 请求:

    @POST("login")
    Call<Post> createPost(@Body Post post);

以及 POJO 类:

    public class Post {
    
        private String anthony;
        private String SUCCESS;
    
        public Post(String name) {
            this.anthony = name;
        }
    
        public String getSUCCESS() {
            return SUCCESS;
        }
    }

对于方法,我使用了以下代码:

    private void createPost() {
            Post post = new Post("mypassword");
            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";
                    content += "S" + postResponse.getSUCCESS();
                    textViewResult.setText(content);
                }
                @Override
                public void onFailure(Call<Post> call, Throwable t) {
                    textViewResult.setText(t.getMessage());
                }
            });
        }

有人知道我的代码有什么问题吗?我预期能够在 "SUCCESS" 的 JSON 对象中获取响应。
英文:

I made a post request method with Retrofit2 but I encountered this problem on my response.

Expected a string but was BEGIN_OBJECT at line 3 column 4 path $.SUCCESS

The response should be

{
&quot;SUCCESS&quot; :
{
&quot;200&quot; : &quot;access granted&quot;,
&quot;ra&quot; : &quot;approved&quot;,
&quot;la&quot; : &quot;approved&quot;,
&quot;ch&quot; : &quot;approved&quot;
}
}

I uses this code for the post request

@POST(&quot;login&quot;)
Call&lt;Post&gt; createPost(@Body Post post);

And for the POJO class

public class Post {

    private String anthony;
    private String SUCCESS;

    public Post(String name) {
        this.anthony = name;
    }

    public String getSUCCESS() {
        return SUCCESS;
    }
}

For the method I use the following code

private void createPost() {
        Post post = new Post(&quot;mypassword&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;;
                content += &quot;S&quot; + postResponse.getSUCCESS();
                textViewResult.setText(content);
            }
            @Override
            public void onFailure(Call&lt;Post&gt; call, Throwable t) {
                textViewResult.setText(t.getMessage());
            }
        });
    }

Does anyone know what's wrong with my code? I expected to get the response inside the "SUCCESS" json object.

答案1

得分: 1

你期望在你想要的响应中,SUCCESS 是一个对象,但是你在 Post 类中将其定义为一个字符串。你应该将 SUCCESS 改为对象。

public class Post {

    private String anthony;
    private PostSuccess SUCCESS;

    public Post(String name) {
        this.anthony = name;
    }

    public PostSuccess getSUCCESS() {
        return SUCCESS;
    }
}

public class PostSuccess {
    @JsonProperty("200")
    private String _200;
    private String ra;
    private String la;
    private String ch;
}
英文:

You expect SUCCESS to be an object in your wanted response but you have defined it as a String in your Post class. You should use an object for SUCCESS instead.

public class Post {

    private String anthony;
    private PostSuccess SUCCESS;

    public Post(String name) {
        this.anthony = name;
    }

    public PostSuccess getSUCCESS() {
        return SUCCESS;
    }
}

public class PostSuccess {
    @JsonProperty(&quot;200&quot;)
    private String _200;
    private String ra;
    private String la;
    private String ch;
}

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

发表评论

匿名网友

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

确定