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

复制代码
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 @   今年我十八岁  阅读(49)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示