繁体   English   中英

安卓改造2 | 在 Recyclerview Kotlin 中显示 JSON 数据

[英]Android retrofit 2 | Display JSON data in Recyclerview Kotlin

我有一个关于如何将我的 json 数据显示到 kotlin 中的回收站视图的问题。 这是我想要在单个回收器视图中显示数据对象值的 JSON 数据。

API 响应:

{
    "statusCode": 200,
    "response": {
        "data": [
            {
                "id": 1,
                "title": "One",
                "service_types": [
                    {
                        "id": 84,
                    },
                    {
                        "id": 34,
                    },        
                ],
                "service_ages": [],
                "cancellation": [],
                "hours_day": []
            },   
        ]
    },
    "message": "Data Retrieved",
    "status": true,
    "errors": []
}

界面:

公共接口服务{

 @GET("services")
fun getServices() : Call<List<GetAllServices>>

}

模型类

获取所有服务

data class GetAllServices(
    val errors: List<Any>,
    val message: String,
    val response: Response,
    val status: Boolean,
    val statusCode: Int
)

请提供解决方案。

更新您的 getServices 功能...

 @GET("services") fun getServices() : Call<GetAllServices>

然后在您的响应侦听器上解析数据对象,将其设置到适配器中

像这样调用 Api

ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
    Call<GetAllServices> call = apiService.getServices();

    call.enqueue(new Callback<GetAllServices>() {
        @Override
        public void onResponse(Call<GetAllServices> call, Response<List<Movie>> response) {
           // YOUR BODY 
response.body();
          
        if(CHECK SERVICE STATUS CODE){
            recyclerAdapter.setMovieList(PASS THE LIST);//PARSE YOUR DATA HERE
         }
        }

        @Override
        public void onFailure(Call<GetAllServices> call, Throwable t) {
            Log.d("TAG","Response = "+t.toString());
        }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM