java - 如何将从 api 收到的响应保存在 arraylist 上?

标签 java android retrofit2

我是改造新手,我想保存来 self 的 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;
    }

我想要填充数组列表。

最佳答案

执行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;

关于java - 如何将从 api 收到的响应保存在 arraylist 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58159344/

相关文章:

java - 使用 ImageView 和 switch case 减少冗余代码

java - 如何在执行 OkHttp 同步请求时获取 HTTP 响应错误代码

java - 为什么我们提出接口(interface)改造请求

java - Hibernate 中两个单向映射与双向映射有何不同?

java - 如何在 Jelastic Tomcat 中引用 DLL 文件

java.io.FileNotFoundException : (Access is denied)

android - 当用户点击 MapView 中的 OverlayItem 时显示一个简单的文本标签/描述气泡

java - 错误 : Could not find or load main class excel2JSON. excel

java - 移动图形路径对象

android - 在照片引用和 key 的帮助下使用改造库获取 body 响应中的照片