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

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

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

问题

  1. 我使用 Retrofit2 创建了一个 POST 请求方法,但在响应中遇到了以下问题:
  2. 预期得到一个字符串,但在第3行第4列处实际得到了一个 BEGIN_OBJECT,路径为 $.SUCCESS
  3. 响应应该是:
  4. {
  5. "SUCCESS" :
  6. {
  7. "200" : "访问已授权",
  8. "ra" : "已批准",
  9. "la" : "已批准",
  10. "ch" : "已批准"
  11. }
  12. }
  13. 我使用了以下代码进行 POST 请求:
  14. @POST("login")
  15. Call<Post> createPost(@Body Post post);
  16. 以及 POJO 类:
  17. public class Post {
  18. private String anthony;
  19. private String SUCCESS;
  20. public Post(String name) {
  21. this.anthony = name;
  22. }
  23. public String getSUCCESS() {
  24. return SUCCESS;
  25. }
  26. }
  27. 对于方法,我使用了以下代码:
  28. private void createPost() {
  29. Post post = new Post("mypassword");
  30. Call<Post> call = jsonPlaceHolderApi.createPost(post);
  31. call.enqueue(new Callback<Post>() {
  32. @Override
  33. public void onResponse(Call<Post> call, Response<Post> response) {
  34. if (!response.isSuccessful()) {
  35. textViewResult.setText("Code: " + response.code());
  36. return;
  37. }
  38. Post postResponse = response.body();
  39. String content = "";
  40. content += "Code: " + response.code() + "\n";
  41. content += "S" + postResponse.getSUCCESS();
  42. textViewResult.setText(content);
  43. }
  44. @Override
  45. public void onFailure(Call<Post> call, Throwable t) {
  46. textViewResult.setText(t.getMessage());
  47. }
  48. });
  49. }
  50. 有人知道我的代码有什么问题吗?我预期能够在 "SUCCESS" JSON 对象中获取响应。
英文:

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

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

The response should be

  1. {
  2. &quot;SUCCESS&quot; :
  3. {
  4. &quot;200&quot; : &quot;access granted&quot;,
  5. &quot;ra&quot; : &quot;approved&quot;,
  6. &quot;la&quot; : &quot;approved&quot;,
  7. &quot;ch&quot; : &quot;approved&quot;
  8. }
  9. }

I uses this code for the post request

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

And for the POJO class

  1. public class Post {
  2. private String anthony;
  3. private String SUCCESS;
  4. public Post(String name) {
  5. this.anthony = name;
  6. }
  7. public String getSUCCESS() {
  8. return SUCCESS;
  9. }
  10. }

For the method I use the following code

  1. private void createPost() {
  2. Post post = new Post(&quot;mypassword&quot;);
  3. Call&lt;Post&gt; call = jsonPlaceHolderApi.createPost(post);
  4. call.enqueue(new Callback&lt;Post&gt;() {
  5. @Override
  6. public void onResponse(Call&lt;Post&gt; call, Response&lt;Post&gt; response) {
  7. if (!response.isSuccessful()) {
  8. textViewResult.setText(&quot;Code: &quot; + response.code());
  9. return;
  10. }
  11. Post postResponse = response.body();
  12. String content = &quot;&quot;;
  13. content += &quot;Code: &quot; + response.code() + &quot;\n&quot;;
  14. content += &quot;S&quot; + postResponse.getSUCCESS();
  15. textViewResult.setText(content);
  16. }
  17. @Override
  18. public void onFailure(Call&lt;Post&gt; call, Throwable t) {
  19. textViewResult.setText(t.getMessage());
  20. }
  21. });
  22. }

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 改为对象。

  1. public class Post {
  2. private String anthony;
  3. private PostSuccess SUCCESS;
  4. public Post(String name) {
  5. this.anthony = name;
  6. }
  7. public PostSuccess getSUCCESS() {
  8. return SUCCESS;
  9. }
  10. }
  11. public class PostSuccess {
  12. @JsonProperty("200")
  13. private String _200;
  14. private String ra;
  15. private String la;
  16. private String ch;
  17. }
英文:

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.

  1. public class Post {
  2. private String anthony;
  3. private PostSuccess SUCCESS;
  4. public Post(String name) {
  5. this.anthony = name;
  6. }
  7. public PostSuccess getSUCCESS() {
  8. return SUCCESS;
  9. }
  10. }
  11. public class PostSuccess {
  12. @JsonProperty(&quot;200&quot;)
  13. private String _200;
  14. private String ra;
  15. private String la;
  16. private String ch;
  17. }

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:

确定