我在翻新(JSON)android中遇到错误

时间:2019-12-17 15:52:49

标签: android retrofit retrofit2

我正在使用改造来获取数据。当我运行我的应用程序时,它将进入onFailure方法下。我不知道我在哪里弄错了。如果需要,我可以提供项目的更多代码文件。帮助将不胜感激。由于我是新手,请帮忙。 我已经阅读了与同一问题相关的其他问题,但没有一个帮助。

以下是我的代码:

public class Description extends Fragment {
private RecyclerView recyclerView;
private DescriptionAdapter adapter;
private List<DescriptionModel> DescriptList;

public Description() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_description, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    /*Create handle for the RetrofitInstance interface*/
    DescriptService service = DescriptClientInstance.getRetrofitInstance().create(DescriptService.class);
    Call<List<DescriptionModel>> call = service.getAllPhotos();
    call.enqueue(new Callback<List<DescriptionModel>>() {
        @Override
        public void onResponse(Call<List<DescriptionModel>> call, Response<List<DescriptionModel>> response) {
            DescriptList=response.body();
            generateDataList(DescriptList);
        }

        @Override
        public void onFailure(Call<List<DescriptionModel>> call, Throwable t) {
            Toast.makeText(getContext(), "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
        }
    });
}
private void generateDataList(List<DescriptionModel> photoList) {
    recyclerView = getView().findViewById(R.id.recyclerview);
    LinearLayoutManager manager = new LinearLayoutManager(getContext());
    recyclerView.setLayoutManager(manager);
    recyclerView.setHasFixedSize(true);
    adapter = new DescriptionAdapter(getContext(),photoList);
    recyclerView.setAdapter(adapter);
}

适配器:

public class DescriptionAdapter extends RecyclerView.Adapter<DescriptionAdapter.CustomViewHolder> {

private List<DescriptionModel> dataList;
private Context context;

public DescriptionAdapter(Context context,List<DescriptionModel> dataList){
    this.context = context;
    this.dataList = dataList;
}

class CustomViewHolder extends RecyclerView.ViewHolder {

    public final View mView;

    TextView txtTitle;
    TextView textView;

    CustomViewHolder(View itemView) {
        super(itemView);
        mView = itemView;

        txtTitle = mView.findViewById(R.id.text1);
    }
}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    View view = layoutInflater.inflate(R.layout.descriptrow, parent, false);
    return new CustomViewHolder(view);
}

@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
    holder.textView.setText(dataList.get(position).getId().toString());
    holder.txtTitle.setText(dataList.get(position).getDescription());



}

@Override
public int getItemCount() {
    return (dataList==null)?0:dataList.size();
}

在R.layout.fragment_description下是我的回收视图 在description_row下,我有textview(实际上是在提取文本)

1 个答案:

答案 0 :(得分:0)

您的REST操作有问题。不要在您的DescriptionAdapter

中查找错误 您从网络收到的

Response映射不正确。 JSON数据不包含您的对象DescriptionModel。这就是为什么您得到错误。确保为REST调用传递所有参数。

或者尝试从Postman等其他平台拨打相同的电话。因此,您可以阅读JSON并查看错误所在。祝你好运!