安卓请求后端初始化列表数据

1、首先在安卓端实现一个接口,用来接收请求,获取列表数据:

public interface GetListDataInterface {
    @GET("/listData")
    Call<List<String>> getListData();
}

2、然后定义一个Retrofit实例,用来发起请求:

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(baseUrl)
        .addConverterFactory(GsonConverterFactory.create())
        .build();

3、接着实例化GetListDataInterface:

GetListDataInterface getListDataInterface = retrofit.create(GetListDataInterface.class);

4、最后发起请求,获取列表数据:

Call<List<String>> call = getListDataInterface.getListData();
call.enqueue(new Callback<List<String>>() {
    @Override
    public void onResponse(Call<List<String>> call, Response<List<String>> response) {
        List<String> listData = response.body();
        // 将获取到的数据设置到列表中
        adapter.setData(listData);
    }

    @Override
    public void onFailure(Call<List<String>> call, Throwable t) {
        Log.e("Error", t.getMessage());
    }
});

 

posted @ 2023-01-24 08:08  今年我十八岁  阅读(45)  评论(0编辑  收藏  举报