【发布时间】:2021-04-07 19:39:26
【问题描述】:
我正在通过搜索圆圈中的城市来制作天气预报应用程序。使用 API api.openweathermap.org。 当我检索“列表”数组并运行我的应用程序时。它显示“列表没有价值”。有人可以帮助我如何检索我搜索的圈子中的所有城市。我的 API 是http://api.openweathermap.org/data/2.5/find?lat=33.5&lon=22.5&cnt=5&appid=83e20e46727fbf09e4dc1f76a2dcce62
try {
content2 = weather.execute("https://api.openweathermap.org/data/2.5/weather?lat=" +
lat + "&lon=" + lon + "&cnt=" + countCountry + "&appid=83e20e46727fbf09e4dc1f76a2dcce62").get();
// for (int i = 0; i < countCountry.length(); i++) {
Log.i("contentData",content2);
//JSON
JSONObject jsonObject = new JSONObject(content2);
String cityList=jsonObject.getString("list");
JSONArray cityArray=new JSONArray(cityList);
for (int i = 0; i < countCountry.length(); i++) {
String cityName = jsonObject.getString("name");
// String cityName = jsonObject.getString("count");
String weatherData = jsonObject.getString("weather");
String mainTemperature = jsonObject.getString("main");
double visibility;
JSONArray array = new JSONArray(weatherData);
String main = "";
String description = "";
String temperature = "";
for (int j = 0; j < array.length(); j++) {
JSONObject weatherPart = array.getJSONObject(j);
main = weatherPart.getString("main");
description = weatherPart.getString("description");
}
//wind cloud
JSONObject jsonObjectWind = jsonObject.getJSONObject("wind");
String wind = jsonObjectWind.getString("speed");
JSONObject jsonObjectCloud = jsonObject.getJSONObject("clouds");
String cloud = jsonObjectCloud.getString("all");
JSONObject jsonObjectSys = jsonObject.getJSONObject("sys");
String countryName = jsonObjectSys.getString("country");
JSONObject mainPart = new JSONObject(mainTemperature);
temperature = mainPart.getString("temp");
visibility = Double.parseDouble(jsonObject.getString("visibility"));
//By default visibility is in meter
int visibiltyInKilometer = (int) visibility / 1000;
Log.i("Temperature", temperature);
/*Log.i("main",main);
Log.i("description",description);*/
String resultText =
"City : " + cityName + "," + countryName +
"\nMain : " + main +
"\nDescription : " + description +
"\nTemperature : " + temperature + "*C" +
"\nVisibility : " + visibiltyInKilometer + " KM" +
"\nWind: " + wind +
"\nCloud: " + cloud;
result.setText(resultText);
//Now we will show this result on screen
}
}
【问题讨论】:
标签: json android-studio