android - 安卓改造2 | 从 JSON 响应中获取列表数据
问题描述
我正在使用改造2。我正在进行 HTTP 调用并获取这种 JSON 数据作为响应:
[
{
"ActualTotalLoadByMonthValue": "4264156.37",
"AreaName": "Hungary",
"AreaTypeCode": "CTY",
"Dataset": "ActualTotalLoad",
"MapCode": "HU",
"Month": "1",
"ResolutionCode": "PT15M",
"Source": "entso-e",
"Year": "2018"
}
]
我想获取这个 JSON 的上下文并将其呈现在一个数组中,但对于初学者来说,我不知道如何获取我的响应的上下文。我尝试使用 response.body() 但它似乎不起作用。有我的代码:
val call = RequestManager.service.getactualtotalload(areaName,resolution, datetype ,"2018")
call.enqueue(object : Callback<List<Response1>> {
override fun onResponse(call: Call<List<Response1>>, response: Response<List<Response1>>)
{
loaderout.visibility = View.GONE
if (response.isSuccessful) {
Log.d("deee","Json " + response.body().toString())
openTableActivity()
} else {
}
}
override fun onFailure(call: Call<List<Response1>>, t: Throwable) {
loaderout.visibility = View.GONE
}
})
而 Response1 是这样的:
public class Response1 {
@SerializedName("ActualTotalLoadByMonthValue")
@Expose
private String actualTotalLoadByMonthValue;
@SerializedName("AreaName")
@Expose
private String areaName;
@SerializedName("AreaTypeCode")
@Expose
private String areaTypeCode;
@SerializedName("Dataset")
@Expose
private String dataset;
@SerializedName("MapCode")
@Expose
private String mapCode;
@SerializedName("Month")
@Expose
private String month;
@SerializedName("ResolutionCode")
@Expose
private String resolutionCode;
@SerializedName("Source")
@Expose
private String source;
@SerializedName("Year")
@Expose
private String year;
public String getActualTotalLoadByMonthValue() {
return actualTotalLoadByMonthValue;
}
public void setActualTotalLoadByMonthValue(String actualTotalLoadByMonthValue) {
this.actualTotalLoadByMonthValue = actualTotalLoadByMonthValue;
}
public String getAreaName() {
return areaName;
}
public void setAreaName(String areaName) {
this.areaName = areaName;
}
public String getAreaTypeCode() {
return areaTypeCode;
}
public void setAreaTypeCode(String areaTypeCode) {
this.areaTypeCode = areaTypeCode;
}
public String getDataset() {
return dataset;
}
public void setDataset(String dataset) {
this.dataset = dataset;
}
public String getMapCode() {
return mapCode;
}
public void setMapCode(String mapCode) {
this.mapCode = mapCode;
}
public String getMonth() {
return month;
}
public void setMonth(String month) {
this.month = month;
}
public String getResolutionCode() {
return resolutionCode;
}
public void setResolutionCode(String resolutionCode) {
this.resolutionCode = resolutionCode;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
还有我的RequestManager:
object RequestManager {
val interceptor = HttpLoggingInterceptor()
val client = OkHttpClient.Builder().addInterceptor(interceptor).build()
init {
//TODO must be None at live
interceptor.level = HttpLoggingInterceptor.Level.BODY
}
val retrofit = Retrofit.Builder()
.baseUrl("http://c6913be7.ngrok.io/energy/api/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
val service = retrofit.create(Api::class.java)
}
关于如何访问数据以及如何将它们传递给表数组布局的任何提示?
解决方案
@ZookKep - response.body() 将返回对象列表。你可以直接使用它。不需要任何其他逻辑。
我的意思是不要在 response.body() 上调用 .toString()。
您可以检查如下日志
val listOfModels = response.body()
if (listOfModels != null) {
for ((index, model) in listOfModels.withIndex()) {
Log.d("----", "----")
Log.d("INDEX $index", "ActualTotalLoadByMonthValue ${model.actualTotalLoadByMonthValue}")
Log.d("INDEX $index", "AreaName ${model.areaName}")
Log.d("INDEX $index", "AreaTypeCode ${model.areaTypeCode}")
Log.d("INDEX $index", "Dataset ${model.dataset}")
Log.d("INDEX $index", "MapCode ${model.mapCode}")
Log.d("INDEX $index", "Month ${model.month}")
Log.d("INDEX $index", "ResolutionCode ${model.resolutionCode}")
Log.d("INDEX $index", "Source ${model.source}")
Log.d("INDEX $index", "Year ${model.year}")
Log.d("----", "----")
}
}
您仍然对此有疑问,请在评论部分告诉我。我很乐意提供帮助:)
推荐阅读
- node.js - 如何让这个全局变量在调用之间保持它的值?
- javascript - React、Formik 和实现可重复的表单字段(作为具有多个字段的表单)
- android - 如何用键盘使页脚上升
- python - 为什么后来 double %% 转换为 single %
- matlab - 默认启用 datacursormode
- html - 绝对定位少了几个像素
- java - 更改片段内的布局参数
- javascript - 接受字符串并返回字数的函数
- vue.js - 使用外部链接和动态属性 Vuetify v-btn
- java - Microsoft Power Query M 公式语言到 sql 查询转换