为什么,我是API的初学者,我正试图从https://reqres.in/api/users?page=2获取数据,我能够成功地获取电子邮件和图片,但名字和姓氏显示为空?
看PIC来理解我在说什么
ActivityForUserBinding binding;
recycle recycle_adapter;
api_interface api;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityForUserBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
api = Retrofit_instance.get_retrofit_for_users().create(api_interface.class);
api.get_users(2).enqueue(new Callback<pojo_for_users>() {
@Override
public void onResponse(Call<pojo_for_users> call, Response<pojo_for_users> response) {
if(response.isSuccessful()){
recycle_adapter = new recycle(response.body().getData());
binding.recycleView.setAdapter(recycle_adapter);
}
}
@Override
public void onFailure(Call<pojo_for_users> call, Throwable t) {
}
});
}
public class recycle extends RecyclerView.Adapter<recycle.MyViewHolder>{
List<pojo_for_users.Datum> list;
public recycle(List<pojo_for_users.Datum> list) {
this.list = list;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.get_user_profile_holder,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.firstName.setText(String.valueOf(list.get(position).getFirstName()));
holder.email.setText(String.valueOf(list.get(position).getEmail()));
holder.lastName.setText(String.valueOf(list.get(position).getLastName()));
Glide.with(getApplicationContext()).load(list.get(position).getAvatar()).centerCrop().placeholder(R.drawable.ic_launcher_background).into(holder.userProfile);
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private ImageView userProfile;
private ConstraintLayout constraintLayout;
private TextView justText;
private TextView justTextt;
private TextView email;
private TextView lastName;
private TextView firstName;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
userProfile = (ImageView) itemView.findViewById(R.id.user_profile);
constraintLayout = (ConstraintLayout) itemView.findViewById(R.id.constraintLayout);
justText = (TextView) itemView.findViewById(R.id.just_text);
justTextt = (TextView) itemView.findViewById(R.id.just_textt);
email = (TextView) itemView.findViewById(R.id.email);
lastName = (TextView) itemView.findViewById(R.id.last_name);
firstName = (TextView) itemView.findViewById(R.id.first_name);
}
}
}
}
这是我的pojo类
public class pojo_for_users {
private Integer page;
private Integer perPage;
private Integer total;
private Integer totalPages;
private List<Datum> data = null;
private Support support;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getPerPage() {
return perPage;
}
public void setPerPage(Integer perPage) {
this.perPage = perPage;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
public Integer getTotalPages() {
return totalPages;
}
public void setTotalPages(Integer totalPages) {
this.totalPages = totalPages;
}
public List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
public Support getSupport() {
return support;
}
public void setSupport(Support support) {
this.support = support;
}
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
public class Datum {
private Integer id;
private String email;
private String firstName;
private String lastName;
private String avatar;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
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 getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
public class Support {
private String url;
private String text;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
}
创建Retrofit实例
public class Retrofit_instance {
private static Retrofit retrofit;
private static Retrofit retrofit1;
private final static String Base_url = "http://universities.hipolabs.com/";
private final static String Base_url_fo_users = "https://reqres.in/api/";
public static Retrofit get_retrofit(){
if (retrofit == null){
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(Base_url)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
public static Retrofit get_retrofit_for_users(){
if (retrofit1 == null){
retrofit1 = new retrofit2.Retrofit.Builder()
.baseUrl(Base_url_fo_users)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit1;
}
}
这是我的api接口
public interface api_interface {
@GET("search?")
Call<List<pojo>>getdata(@Query("country") String Country);
@GET("users")
Call<pojo_for_users>get_users(
@Query("page") int page_number
);
}
提前感谢。
这是因为pojo_for_users.Datum
类中的属性名称与API中的JSON属性名称不完全匹配。你需要使用"first_name"而不是"firstName"one_answers"last_name"而不是"lastname"。或者,您可以使用注释来指定您希望使用的名称和API使用的名称:
@SerializedName("last_name") //API uses this
private String lastName; // but in your code you can use this
根据您的JSON响应,firstName和lastName具有如下下划线。像下面这样修改代码后,一切都会好起来的。
private String first_name;
private String last_name;
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
Retrofit
使用Gson
将原始JSON
转换为Java对象。转换时,Gson
在Java类中查找API返回的字段名。
基本上,您必须告诉Gson
将值映射到哪里。您可以通过以下两种方式实现:
-
使用确切的
JSON
字段名命名变量@Expose private String first_name; @Expose private String last_name;
请注意,您需要添加
@Expose
注释,以避免在混淆期间更改变量名称(当您的build.gradle
中有minifiyEnabled true
时)。 -
为变量添加
@SerializedName
注释。@SerializedName("first_name") private String firstName; @SerializedName("last_name") private String lastName;