如何将我的 okhttp 响应转换为 Class 或 List<ClassName>?
How do i convert my okhttp response to Class or List<ClassName>?
我是 android 的新手,正在学习 android。
我在程序中使用 Okhttp 库进行 httpcalls。我已经为简单获取请求休息 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。
经过大量搜索后我找到了答案,无需更改服务器代码。
String jsonOutput = response;
Type listType = new TypeToken<List<DemoClass>>(){}.getType();
List<DemoClass> posts = gson.fromJson(jsonOutput, listType);
我是 android 的新手,正在学习 android。 我在程序中使用 Okhttp 库进行 httpcalls。我已经为简单获取请求休息 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。
经过大量搜索后我找到了答案,无需更改服务器代码。
String jsonOutput = response;
Type listType = new TypeToken<List<DemoClass>>(){}.getType();
List<DemoClass> posts = gson.fromJson(jsonOutput, listType);