[英]Gson error expected begin_object but was string at line 1 column 1 path $
[英]Expected a string but was BEGIN_OBJECT at line 3 column 4 path $.SUCCESS
我用 Retrofit2 制作了一個 post 請求方法,但在我的響應中遇到了這個問題。
Expected a string but was BEGIN_OBJECT at line 3 column 4 path $.SUCCESS
回應應該是
{
"SUCCESS" :
{
"200" : "access granted",
"ra" : "approved",
"la" : "approved",
"ch" : "approved"
}
}
我將此代碼用於發布請求
@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 對象中得到響應。
您希望 SUCCESS 成為您想要的響應中的對象,但您已在 Post 類中將其定義為 String。 你應該使用一個對象來代替 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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.