安卓如何在Java中使用具有多个类似参数的JSON请求/响应循环?
当我得到非常相似的值时,有没有一种方法可以将我的请求放入循环中并使用我的响应?我考虑过for循环,但找不到实现它的方法
我正在从Android设备发送请求,有TextView
在等待响应。当我得到响应时,我正在检查它是否为空或null,如果不是,我正在设置TextView
的文本并更改可见性(因为默认设置已消失)
例如,这将是我的JSON
{
spaghetti: [
{
strIngredient1: "1 pound ground beef",
strIngredient2: "1 onion, chopped",
strIngredient3: "null",
strIngredient4: "1 (28 ounce) can diced tomatoes",
strIngredient5: ""
}
]
}
我的情况是:
if(!"".equals(dinner.getStrIngredient1()) && !"null".equals(dish.getStrIngredient1())) {
firstIngredient.setText(dinner.getStrIngredient1());
firstIngredient.setVisibility(View.VISIBLE);
}
if(!"".equals(dinner.getStrIngredient2()) && !"null".equals(dish.getStrIngredient2())) {
secondIngredient.setText(dinner.getStrIngredient2());
secondngredient.setVisibility(View.VISIBLE);
}
...
正如你在上面看到的,我得到了相似的值,我真的需要重复五次吗?有没有办法让它“干净”
# 1 楼答案
使用java反射。 但要做到这一点,你需要调整你的第一个配料,第二个配料分别命名为Ingredit1和Ingredit2
在你的配料列表上找到一个for循环 然后使用java反射动态地形成方法
# 2 楼答案
我想是的,或者你可以用switch-case,但文字也一样