[英]How can i save on arraylist the response that i receive from my api?
我是 retrofit 的新手,我想將 api 的響應保存在數組列表中,就像 object 一樣。
我已經搜索了解決方案,但我不知道回調方法是如何工作的,我也不能很好地理解。
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.