我有一个返回的请求:
[{"user_id":"1","username":"user1","name":"Name1","profile_pic":"user1.jpg"},
{"user_id":"2","username":"user2","name":"Name2","profile_pic":"user2.jpg"},
{"user_id":"3","username":"user3","name":"Name3","profile_pic":"user3.jpg"}]
我想创建一个执行该调用的改造项目,所以我创建了:
public interface ApiServiceUsers {
@GET("/api/users")
void getUsers(Callback<PojoUsers> callback);
}
public class PojoUsers {
private String user_id;
private String username;
private String name;
private String profile_pic;
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProfile_pic() {
return profile_pic;
}
public void setProfile_pic(String profile_pic) {
this.profile_pic = profile_pic;
}
}
RestClient 类:
public class RestClientUsers {
private static final String BASE_URL = "http://www.myurl.test/project";
private ApiServiceUsers apiServiceUsers;
public RestClientUsers() {
Gson gson = new GsonBuilder()
.setDateFormat("dd'-'MM'-'yyyy'T'HH':'mm':'ss'.'SSS'Z'")
.create();
RestAdapter restAdapter = new RestAdapter.Builder()
.setLogLevel(RestAdapter.LogLevel.FULL)
.setEndpoint(BASE_URL)
.setConverter(new GsonConverter(gson))
.build();
apiServiceUsers = restAdapter.create(ApiServiceUsers.class);
}
public void getUsers(final Callback<PojoUsers> callback) {
apiServiceUsers.getUsers(callback);
}
public ApiServiceUsers getApiServiceUsers() {
return apiServiceUsers;
}
}
最后,在主Activity中:
RestClientUsers restClientUsers = new RestClientUsers();
restClientUsers.getUsers(new Callback<PojoUsers>() {
@Override
public void success(PojoUsers pojoUsers, Response response) {
Log.i("RESPONSE OK", pojoUsers.getName());
}
@Override
public void failure(RetrofitError error) {
Log.i("RESPONSE ERROR", error.getMessage());
}
});
但是这个日志完全没有。
通过改造调试我可以看到请求是好的,请求的结果也是用户列表,但回调似乎不起作用。
我该如何解决?以及如何将所有 3 个用户存储在像数组这样的结构中?提前致谢。