Android:<Class>如何使用改造从列表中获取对象属性



请帮我解决这个问题。我想通过REST API从JSON对象中获取一些数据。

以下是json:的演示

{
"itemList": [
{
"name": "AF1_AC",
"value": false,
"awid": "1",
"sys": {
"cts": "2018-07-16T14:51:34.166Z",
"mts": "2018-07-16T17:49:40.206Z",
"rev": 775
},
"id": "5b4cb0f66a4c333860d64d79"
},
{
"name": "ST1_RE",
"value": 27.11,
"awid": "1",
"sys": {
"cts": "2018-07-15T14:53:05.228Z",
"mts": "2018-07-16T17:49:40.320Z",
"rev": 4124
}
}
]
}

这是我的界面:

public interface FarmAPI {
String BASE_URL = "http://10.0.2.2:6221/";
@GET("smartfarm/0-1/listFarmObjectValue") Call<ItemList> getItemList();
class Factory {
private static FarmAPI service;
public static FarmAPI getInstance() {
if (service == null) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().
addInterceptor(interceptor)
.build();
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.client(client).build();
service = retrofit.create(FarmAPI.class);
return service;
} else {
return service;
}
}
}

}

以下是jsonschema2pojo生成的类:

public class ListFarmObjectValue {
@SerializedName("itemList")
@Expose
private List<ItemList> itemList = null;
public List<ItemList> getItemList() {
return itemList;
}
public void setItemList(List<ItemList> itemList) {
this.itemList = itemList;
}

}

第二种:

public class ItemList {
@SerializedName("name")
@Expose
private String name;
@SerializedName("value")
@Expose
private double value;
@SerializedName("awid")
@Expose
private String awid;
@SerializedName("id")
@Expose
private String id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
public String getAwid() {
return awid;
}
public void setAwid(String awid) {
this.awid = awid;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}

}

现在我想从对象中获取值,其中name=ST1_RE,并将值设置为特定的TextView。在方法onCreate:中

FarmAPI.Factory.getInstance().getItemList().enqueue(new Callback<ItemList>() {
@Override
public void onResponse(Call<ItemList> call, Response<ItemList> response) {
if (response.body().getName().equals("ST1_RE")) {
realTemperature.setText(response.body().getValue() + "");
}
}
@Override
public void onFailure(Call<ItemList> call, Throwable t) {
t.printStackTrace();
}
});

但它不起作用:(你能告诉我在这种情况下我该怎么做吗?或者,如果更好的话,可以不通过jsonschema2pojo生成类。。。?我看到了一些例子,但它对我来说是一个新的类别,我不理解它。我也不想改变FarmAPI接口,因为我在其他情况下使用它。

非常感谢!

1(更改API调用的预期类型

@GET("smartfarm/0-1/listFarmObjectValue") Call<ItemList> getItemList();

@GET("smartfarm/0-1/listFarmObjectValue") Call< ListFarmObjectValue > getItemList();

2( 遍历ItemList对象列表,找到名称为"ST1_RE"的对象

FarmAPI.Factory.getInstance().getItemList().enqueue(new Callback<ListFarmObjectValue>() { @Override public void onResponse(Call<ListFarmObjectValue> call, Response<ListFarmObjectValue> response) { ListFarmObjectValue value = response.body(); for(ItemList item : value.getItemList()){ if (item.getName().equals("ST1_RE")) { realTemperature.setText(response.body().getValue() + ""); break; } } } });

编辑:我刚刚看到你的消息,你不想更改API调用。在这种情况下,只需在同一个类中创建一个具有新返回类型的新方法,正如我所提到的。

最新更新