API成功调用,但未显示

时间:2018-02-06 07:58:55

标签: android api retrofit2

我正在尝试使用改造调用本地API服务器,在我的Logcat中显示调用了API成功,但在我的应用程序中没有显示任何内容。我怎么能解决这个问题?这是我的代码:

interface.java:

public interface Service {
 @GET("vocabulary/id/*")
 Call<APIResponse<List<VocabMaster>>> getVocabMaster();
}

Vocab Master.java从API收集数据:

public class VocabMaster implements Serializable {

private BigDecimal vocab_id;
private String hanzi_t;
private String hanzi_s;
private String pinyin;
private BigDecimal level_id;
private List<VocabMeaning> meaning;

public BigDecimal getVocab_id() {
    return vocab_id;
}

public void setVocab_id(BigDecimal vocab_id) {
    this.vocab_id = vocab_id;
}

public String getHanzi_t() {
    return hanzi_t;
}

public void setHanzi_t(String hanzi_t) {
    this.hanzi_t = hanzi_t;
}

public String getHanzi_s() {
    return hanzi_s;
}

public void setHanzi_s(String hanzi_s) {
    this.hanzi_s = hanzi_s;
}

public String getPinyin() {
    return pinyin;
}

public void setPinyin(String pinyin) {
    this.pinyin = pinyin;
}

public List<VocabMeaning> getMeaning() {
    return meaning;
}

public void setMeaning(List<VocabMeaning> meaning) {
    this.meaning = meaning;
}

public BigDecimal getLevel_id() {
    return level_id;
}

public void setLevel_id(BigDecimal level_id) {
    this.level_id = level_id;
}

@Override
public String toString() {
    return "VocabMaster{" +
            "vocab_id=" + vocab_id +
            ", hanzi_t='" + hanzi_t + '\'' +
            ", hanzi_s='" + hanzi_s + '\'' +
            ", pinyin='" + pinyin + '\'' +
            ", level_id=" + level_id +
            ", meaning=" + meaning +
            '}';
  }
}

这是MainActivity.java:

 public class MainActivity extends AppCompatActivity {
RecyclerView rvReligiVideo;
List<VocabMaster> vocabMasters = new ArrayList<>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rvReligiVideo = findViewById(R.id.rvReligi);
    initReligiVideo();
    loadReligiVideo();

}

private void initReligiVideo() {
    LinearLayoutManager layout = new LinearLayoutManager(this);
    layout.setOrientation(LinearLayoutManager.HORIZONTAL);
    rvReligiVideo.setLayoutManager(layout);
    VocabAdapter ar = new VocabAdapter(vocabMasters);
    rvReligiVideo.setAdapter(ar);

}

//=========== Request to API ==========
private void loadReligiVideo() {
    Call<APIResponse<List<VocabMaster>>> call = ServicesFactory.getService().getVocabMaster();
    call.enqueue(new Callback<APIResponse<List<VocabMaster>>>() {
        @Override
        public void onResponse(Call<APIResponse<List<VocabMaster>>> call, Response<APIResponse<List<VocabMaster>>> response) {
            if (response.isSuccessful() && response.body().isSuccessful()) {
                List<VocabMaster> data = response.body().data;
                if (data != null) {
                    vocabMasters.clear();
                    vocabMasters.addAll(data);
                    rvReligiVideo.getAdapter().notifyDataSetChanged();
                }
            } else {
                Toast.makeText(MainActivity.this, response.errorBody().toString(), Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onFailure(Call<APIResponse<List<VocabMaster>>> call, Throwable t) {
            Toast.makeText(MainActivity.this, t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
        }

    });
  }

}

android studio上的logcat:

enter image description here

JSON数据结构:

enter image description here

这是我的适配器:

public class VocabAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
List<VocabMaster> data;

public VocabAdapter(List<VocabMaster> data) {
    this.data = data;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.vocabulary_model, null);
    return new VH(v);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    VocabMaster vm = data.get(position);
    VH vh = (VH) holder;

    vh.hanziS.setText(vm.getHanzi_s());
    vh.hanziT.setText(vm.getHanzi_t());
    vh.pinyin.setText(vm.getPinyin());

    vh.sound.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(v.getContext(), "Sound", Toast.LENGTH_SHORT).show();
        }
    });
    vh.favorite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(v.getContext(), "Favorite", Toast.LENGTH_SHORT).show();
        }
    });
    vh.share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(v.getContext(), "Share", Toast.LENGTH_SHORT).show();
        }
    });
}

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

public class VH extends RecyclerView.ViewHolder {
    TextView hanziS;
    TextView hanziT;
    TextView pinyin;
    TextView desc;
    ImageView sound, share, favorite;
    Context context;

    public VH(View itemView) {
        super(itemView);

        hanziS = itemView.findViewById(R.id.hanziS);
        hanziT = itemView.findViewById(R.id.hanziT);
        pinyin = itemView.findViewById(R.id.pinyin);
        desc = itemView.findViewById(R.id.txtDesc);
        sound = itemView.findViewById(R.id.imgSpeaker);
        share = itemView.findViewById(R.id.imgShare);
        favorite = itemView.findViewById(R.id.imgFavotite);
    }

}

}

2 个答案:

答案 0 :(得分:0)

我在您的代码中做了一些更改...使用此

  private VocabAdapter ar;
private void initReligiVideo() {
    LinearLayoutManager layout = new LinearLayoutManager(this);
    layout.setOrientation(LinearLayoutManager.HORIZONTAL);
    rvReligiVideo.setLayoutManager(layout);
     ar = new VocabAdapter(vocabMasters);
    rvReligiVideo.setAdapter(ar);
}

    private void loadReligiVideo() {
    Call<APIResponse<List<VocabMaster>>> call = ServicesFactory.getService().getVocabMaster();
    call.enqueue(new Callback<APIResponse<List<VocabMaster>>>() {
        @Override
        public void onResponse(Call<APIResponse<List<VocabMaster>>> call, Response<APIResponse<List<VocabMaster>>> response) {
            if (response.isSuccessful() && response.body().isSuccessful()) {
                List<VocabMaster> data = response.body().data;
                if (data != null) {
                    ar.setVocoList(data);
                }
            } else {
                Toast.makeText(MainActivity.this, response.errorBody().toString(), Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onFailure(Call<APIResponse<List<VocabMaster>>> call, Throwable t) {
            Toast.makeText(MainActivity.this, t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
        }

    });
}

在适配器类

中添加此方法
 public void setVocoList(List<VocabMaster> list){
    if(list!=null)
        data=list;
    notifyDataSetChanged();
}

答案 1 :(得分:0)

拥有新列表时,请勿更改适配器列表。

   //incorrect except that the adapter is first time set to recyclerview and setVocoList is called before recyclerview.setAdater
    public void setVocoList(List<VocabMaster> list){
        if(list!=null)
            data=list;
        notifyDataSetChanged();
    }

    //correct
    private List<VocabMaster>  datas = new ArrayList()
    public void setVocoList(List<VocabMaster> list){
            if(list==null) return;
            datas.clear();
            datas.addAll(list);
            notifyDataSetChanged();
        }