我如何将我的okhttp响应转换为Class或List ?

问题描述 投票:1回答:1

我是android的新手,正在学习android。我正在使用Okhttp库进行程序中的httpcall。我已经为“简单获取请求”制作了REST API。

api / FeaturedCourses(URL)

此我的回复

[
  {
    "$id": "1",
    "ExamId": 44,
    "ImageName": "637064732146027131.jpg",
    "ExamName": "Some Exam Name",
    "Description": "Some Long Description Goes Here.",
    "SellPriceUSD": 10,
    "SellPriceINR": 490
  }
]

在okhttp中,我已经做到这里了。

            @Override
            public void onResponse(Call call, Response response) throws IOException {    
                String data = response.body().string();
                nsjdemo.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        txtString.setText(data);
                    }
                });
            }

这将在TextBox中显示API响应。但我想将Api响应绑定到列表。所以以后我可以将它绑定到recyclerview或listview。

java android gson okhttp
1个回答
0
投票

您可以使用Gson将json转换为对象,反之亦然。您可以在这里阅读更多内容:

https://guides.codepath.com/android/leveraging-the-gson-library

© www.soinside.com 2019 - 2024. All rights reserved.