Ahm*_*kol 2 java android json foursquare
我正在从foursquare 获取场地列表,我需要获得的每个场地的名称、地址和城市。这里我的问题是,如果地址或城市为空,则会出现错误并显示“JSON 异常:地址无值”。我尝试了不同的方法来修复它,但无法解决问题。我希望你能帮助我。谢谢你。
JSONObject b = venues.getJSONObject(i);
JSONObject location = b.getJSONObject("location");
String name = "";
String address = "";
String city = "";
if ("".equals(b.get("name"))) {
// Toast.makeText(Activity2.this,"Name of Place: " +name+ " Location: "+country, Toast.LENGTH_SHORT).show();
name = "No place ";
} else {
name = b.getString("name");
}
if ("".equals(location.getString("address"))) {
address = "No address";
} else {
address = location.getString("address");
}
if ("".equals(location.getString("city"))) {
city = "No city";
} else {
city = location.getString("city");
}
name = "Name of the place: " + name;
// + " Address: "+ address + " City: " + city;
list.add(name);
Run Code Online (Sandbox Code Playgroud)
您可能需要在调用 getString 之前调用 has() 方法以避免异常。但是还有一种更简单的方法 - 只需使用optString而不是 getString,如果该值不存在,它将返回“回退”字符串。
String name = location.optString("name", "No Place");
String address = location.optString("address", "No Address");
String city = location.optString("city", "No City");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7160 次 |
| 最近记录: |