我在加载时遇到麻烦想从JSON中检索url数据
{
"status": "success",
"message": "This is a message",
"item":{
"id":"1",
"video":{
"url" : [
"https://url1.com",
"https://url1.com",
"https://url1.com"
]
}
}
}
ResponseData
中public class ResponseData {
@SerializedName("status")
private String status;
@SerializedName("message")
private String message;
@SerializedName("item")
private Item item;
//Getters
}
项public class Item {
@SerializedName("id")
private String id;
@SerializedName("video")
private Video video;
//Getters
}
视频public class Video {
@SerializedName("url")
private List<String> urlList;
//Getters
}
在此之后我应该怎么做才能获得每个URL并在Retrofit onResponse中应用它?
@Override
public void onResponse(@NonNull Call<ResponseData> call, @NonNull Response<ResponseData> response) {
if (response.isSuccessful()) {
ResponseData resp = response.body();
//For the call I want results like the method below
DataFromServer d = new DataFromServer();
d.url = resp.getItem().getVideo().getUrlList().getUrl1(); // I want it like this
}
}
resp.getItem().getVideo().getUrlList().forEach((url) -> {
Log.i("url", url) // you can get each url here
}
);
你基本上是从服务器得到一个列表。
不需要键来访问它们。您可以将它们存储在列表中,也可以执行
list.get(0) -查询第一个url
list.get(1) -查询第二个url
等等