我是改造的新手,我想将来自api的响应保存为数组列表中的对象。
我有搜索解决方案,但我不知道回调方法的工作原理,我不太了解。
public ArrayList<Match> recolectar_partido(){
final ArrayList<Match> datos=new ArrayList<Match>();
Call<List<MatchResponse>> call = RetrofitClient.getInstance().getApi().getmatch();
call.enqueue(new Callback<List<MatchResponse>>() {
@Override
public void onResponse(Call<List<MatchResponse>> call, Response<List<MatchResponse>> response) {
matchlist=response.body();
for (MatchResponse fix:matchlist) {
Integer idfix=fix.getId_fixture();
Integer idsta=fix.getId_stadium();
String fecha=fix.getFecha();
String hora=fix.getHora();
Match variable= new Match(idfix,idsta,fecha,hora);
datos.add(variable);
}
}
@Override
public void onFailure(Call<List<MatchResponse>> call, Throwable t) {
Toast.makeText(getApplicationContext(),"error de conexion",Toast.LENGTH_SHORT).show();
}
});
return datos;
}
我想把arraylist填满。
最佳答案
执行call.execute()
而不是enqueue
。
例如。
final ArrayList<Match> datos=new ArrayList<Match>();
Call<List<MatchResponse>> call = RetrofitClient.getInstance().getApi().getmatch();
matchlist= call.execute().body();
for (MatchResponse fix:matchlist) {
Integer idfix=fix.getId_fixture();
Integer idsta=fix.getId_stadium();
String fecha=fix.getFecha();
String hora=fix.getHora();
Match variable= new Match(idfix,idsta,fecha,hora);
datos.add(variable);
}
return datos;