Android:如何从列表中获取对象属性<;类别>;使用改型

Android:如何从列表中获取对象属性<;类别>;使用改型,android,json,list,retrofit,Android,Json,List,Retrofit,请帮我解决这个问题。我想通过RESTAPI从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",

请帮我解决这个问题。我想通过RESTAPI从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;
        }
    }
}
}

现在我想从object中获取值,其中name=ST1\u 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();
        }
    });
FarmAPI.Factory.getInstance().getItemList().enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.body().getName().equals(“ST1_-RE”)){
realTemperature.setText(response.body().getValue()+);
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
t、 printStackTrace();
}
});
但它不起作用:( 你能告诉我在这种情况下我该怎么做吗?或者如果不通过jsonschema2pojo生成类更好的话…?我看到了一些示例,但它是我的新类别,我不理解它。而且我不想更改FarmAPI接口,因为我在其他情况下使用它

非常感谢!更改API调用的预期类型

@GET(“smartfarm/0-1/listFarmObjectValue”)调用getItemList()

@GET(“smartfarm/0-1/listFarmObjectValue”)调用getItemList()

2) 遍历ItemList对象列表以查找名称为“ST1\u RE”的对象


FarmAPI.Factory.getInstance().getItemList().enqueue(新回调()函数){
@凌驾
公共void onResponse(调用、响应){
ListFarmObjectValue=response.body();
对于(ItemList项:value.getItemList()){
if(item.getName().equals(“ST1_-RE”)){
realTemperature.setText(response.body().getValue()+);
打破
}
}
}
});

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

非常感谢您的快速回复,但它不起作用。我更改了realTemperature.setText(response.body().getValue()+);到realTemperature.setText(item.getValue()+);但是TextView并没有改变。但在控制台中,有正确的数据。生成的类中可能存在问题。你能告诉我如何用我自己为这个json创建的类做到这一点吗?非常感谢。在onRespose()方法中放置一个调试点,查看它是否按照预期完成了所有情况。另外,看看这是否是一个UI问题,比如TextView由于某些原因不可见。将硬编码文本设置到文本视图,以确认UI中的所有内容都正常。它跳转到onFailure方法。我看到异常:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为双精度,但在第1行第40列路径$.itemList[0]处为BEGIN_对象。值似乎不能为布尔值,也不能为双精度。但是在json中有两种类型的值——布尔值和双精度值。请问,有没有办法做到这一点?谢谢您您可以将“value”类型设置为“Object”而不是“double”,并且可以在使用“instanceof”检查类型后对其进行适当的强制转换。但只能强制转换为双精度或布尔值,而不能转换为基本的双精度/布尔值。
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;
}
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();
        }
    });