我是Retrofit Lib的新手..但是我读了它的功能所以我受到启发使用这个...我使用了This link并且我得到了如下的响应
{
"Users":[
{
"firstname":"Mike",
"lastname":"Dalisay",
"username":"mike143"
},
{
"firstname":"Jemski",
"lastname":"Panlilios",
"username":"jemboy09"
},
我也在界面
中完成了这个public void GetUser(Callback<List<Users>> respose);
但获得04-28 07:05:13.613 32242-32242/package name I/System.out﹕ error.toString() = retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
我的Users.class
public class Users {
// @SerializedName(value="Users")
String firstname;
String astname;
String username;
public List<Users> user;
public void setUsers(List<Users> users) {
this.user = users ;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAstname() {
return astname;
}
public void setAstname(String astname) {
this.astname = astname;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
}
我按照yozzy的回答编辑了我的课程并将其称为
api.GetUser(new Callback<UserResponse>() {
@Override
public void success(UserResponse userResponse, Response response) {
System.out.println(response.toString());
loading.dismiss();
}
public void failure(RetrofitError error) {
System.out.println("error.toString() = " + error.toString());
}
});
但获得retrofit.client.Response@532aa440
答案 0 :(得分:1)
您的JSON响应以{所以它是一个对象开头。
您的界面应该是这样的:
public void GetUser(Callback<UserResponse> response);
你的模型UserResponse应该是这样的:
public class UserResponse implements Serializable {
private ArrayList<User> Users;
public ArrayList<User> getUsers() {
return Users;
}
public void setUsers(ArrayList<User> Users) {
this.Users = Users;
}
}
最后是你的用户模型:
public class User implements Serializable {
private String firstname;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
}
顺便提一下Retrofit已更新,请看这个链接:http://square.github.io/retrofit/
答案 1 :(得分:0)
Public class Result{
private List<Users> Users;
}
public class Users {
private String firstname;
private String lastname;
private String username;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
并使用Result class
。
public void GetUser(Callback<Result> respose);