Android RecyclerView适配不同JSON结构崩溃问题修复指导
适配URL2 JSON结构的修改方案
1. 重构数据Model类
URL2的根JSON结构和URL1完全不同,需要重新定义对应的数据模型:
// 根响应类,对应URL2返回的整体结构 public class Url2DataResponse { private Films films; // 按URL2中films对象的实际字段定义该类 private Map<String, List<String>> categories; // 适配"分类名-影片数组"的键值对结构 // Getter、Setter方法 } // 对应URL2中films对象的实体类 public class Films { // 根据接口返回的films字段添加对应属性,例: private String id; private String name; // Getter、Setter方法 }
如果需要后续用列表形式展示分类数据,可以额外定义一个CategoryItem类,用来封装分类名和对应影片列表,方便Adapter使用:
public class CategoryItem { private String categoryName; private List<String> filmList; public CategoryItem(String categoryName, List<String> filmList) { this.categoryName = categoryName; this.filmList = filmList; } // Getter方法 }
2. 修改Retrofit API接口定义
把原来适配URL1的接口返回类型替换为新的根Model类:
public interface ApiService { // 替换原有接口方法,适配URL2 @GET("url2的接口路径") Call<Url2DataResponse> fetchUrl2Data(); }
3. 调整Activity中的数据处理逻辑
在Retrofit的回调中,把Map结构的categories转换为Adapter需要的列表格式:
apiService.fetchUrl2Data().enqueue(new Callback<Url2DataResponse>() { @Override public void onResponse(Call<Url2DataResponse> call, Response<Url2DataResponse> response) { if (response.isSuccessful() && response.body() != null) { Map<String, List<String>> categoryMap = response.body().getCategories(); List<CategoryItem> categoryItems = new ArrayList<>(); // 遍历Map,转换为Adapter可用的列表 for (Map.Entry<String, List<String>> entry : categoryMap.entrySet()) { categoryItems.add(new CategoryItem(entry.getKey(), entry.getValue())); } // 更新Adapter数据 categoryAdapter.setData(categoryItems); } } @Override public void onFailure(Call<Url2DataResponse> call, Throwable t) { // 保留或优化失败提示逻辑 } });
4. 更新RecyclerView Adapter
适配新的CategoryItem数据结构,调整视图绑定逻辑:
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> { private List<CategoryItem> mData; public CategoryAdapter(List<CategoryItem> data) { this.mData = data; } public void setData(List<CategoryItem> data) { this.mData = data; notifyDataSetChanged(); } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_category, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { CategoryItem item = mData.get(position); holder.tvCategoryName.setText(item.getCategoryName()); // 把影片列表转为字符串展示,或用嵌套RecyclerView展示 String filmStr = TextUtils.join("、", item.getFilmList()); holder.tvFilms.setText(filmStr); } @Override public int getItemCount() { return mData != null ? mData.size() : 0; } static class ViewHolder extends RecyclerView.ViewHolder { TextView tvCategoryName; TextView tvFilms; public ViewHolder(@NonNull View itemView) { super(itemView); tvCategoryName = itemView.findViewById(R.id.tv_category_name); tvFilms = itemView.findViewById(R.id.tv_films); } } }
5. 可选:优化Gson解析配置
如果URL2的JSON存在格式不规范(比如空值、大小写不匹配),可以给Gson添加宽松解析配置:
Gson gson = new GsonBuilder() .setLenient() .serializeNulls() .create(); // 构建Retrofit时使用该Gson实例 Retrofit retrofit = new Retrofit.Builder() .baseUrl("你的基础URL") .addConverterFactory(GsonConverterFactory.create(gson)) .build();
内容的提问来源于stack exchange,提问作者Ali Ghayed
相关产品推荐
相关产品推荐

