我已经编写了这个函数来使用 openweather API 获取 JSON 天气数据。我有错误 org.json.JSONException: No value for temp while using OWM_TEMPARATURE 但是当我根本不使用 temparature 字段时,我得到了正确的日期和描述。
private String[] getWeatherDataFromJson(String forecastJsonStr, int numDays)
throws JSONException {
final String OWM_LIST = "list";
final String OWM_WEATHER = "weather";
final String OWM_TEMPERATURE = "temp";
final String OWM_MAX = "max";
final String OWM_MIN = "min";
final String OWM_DESCRIPTION = "main";
JSONObject forecastJson = new JSONObject(forecastJsonStr);
JSONArray weatherArray = forecastJson.getJSONArray(OWM_LIST);
Time dayTime = new Time();
dayTime.setToNow();
int julianStartDay = Time.getJulianDay(System.currentTimeMillis(), dayTime.gmtoff);
dayTime = new Time();
String[] resultStrs = new String[numDays];
for(int i = 0; i < weatherArray.length(); i++) {
String day;
String description;
String highAndLow;
JSONObject dayForecast = weatherArray.getJSONObject(i);
long dateTime;
dateTime = dayTime.setJulianDay(julianStartDay+i);
day = getReadableDateString(dateTime);
JSONObject weatherObject = dayForecast.getJSONArray(OWM_WEATHER).getJSONObject(0);
description = weatherObject.getString(OWM_DESCRIPTION);
JSONObject temperatureObject = dayForecast.getJSONObject(OWM_TEMPERATURE);
double high = temperatureObject.getDouble(OWM_MAX);
double low = temperatureObject.getDouble(OWM_MIN);
highAndLow = formatHighLows(high, low);
resultStrs[i] = day + " - " + description + " - " + highAndLow;
}
for (String s : resultStrs) {
Log.v(LOG_TAG, "Forecast entry: " + s);
}
return resultStrs;
}
日志猫:
没有温度值
org.json.JSONException:在 com.example.abhishekkumar.sunshine.app 的 org.json.JSONObject.getJSONObject(JSONObject.java:609) 的 org.json.JSONObject.get(JSONObject.java:389) 中没有 temp 值.ForecastFragment$FetchWeatherTask.getWeatherDataFromJson(ForecastFragment.java:177) 在 com.example.abhishekkumar.sunshine.app.ForecastFragment$FetchWeatherTask.doInBackground(ForecastFragment.java:279) 在 com.example.abhishekkumar.sunshine.app.ForecastFragment$FetchWeatherTask.doInBackground(ForecastFragment.java:92) 在 android.os.AsyncTask$2.call(AsyncTask.java:295) 在 java.util.concurrent.FutureTask。在 android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 在 java.lang.Thread.run(Thread.java:818)
我的开放天气数据是:
{"city":{"id":1260788,"name":"Pālam","coord":{"lon":77.099998,"lat":28.566669},"country":"IN","population":0,"sys":{"population":0}},"cod":"200","message":0.0155,"cnt":7,"list":[{"dt":1467018000,"main":{"temp":38.42,"temp_min":35.56,"temp_max":38.42,"pressure":986.68,"sea_level":1010.85,"grnd_level":986.68,"humidity":62,"temp_kf":2.85},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":36},"wind":{"speed":4.17,"deg":107.001},"sys":{"pod":"d"},"dt_txt":"2016-06-27 09:00:00"},{"dt":1467028800,"main":{"temp":33.82,"temp_min":31.92,"temp_max":33.82,"pressure":985.98,"sea_level":1009.98,"grnd_level":985.98,"humidity":76,"temp_kf":1.9},"weather":[{"id":501,"main":"Rain","description":"moderate rain","icon":"10d"}],"clouds":{"all":32},"wind":{"speed":3.11,"deg":99.5087},"rain":{"3h":5.54},"sys":{"pod":"d"},"dt_txt":"2016-06-27 12:00:00"},{"dt":1467039600,"main":{"temp":31.34,"temp_min":30.39,"temp_max":31.34,"pressure":987.55,"sea_level":1011.75,"grnd_level":987.55,"humidity":75,"temp_kf":0.95},"weather":[{"id":501,"main":"Rain","description":"moderate rain","icon":"10n"}],"clouds":{"all":36},"wind":{"speed":2.31,"deg":105.002},"rain":{"3h":4.07},"sys":{"pod":"n"},"dt_txt":"2016-06-27 15:00:00"},{"dt":1467050400,"main":{"temp":30.15,"temp_min":30.15,"temp_max":30.15,"pressure":988.78,"sea_level":1013.04,"grnd_level":988.78,"humidity":78,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":32},"wind":{"speed":3.22,"deg":100.501},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-06-27 18:00:00"},{"dt":1467061200,"main":{"temp":29.52,"temp_min":29.52,"temp_max":29.52,"pressure":987.7,"sea_level":1012.15,"grnd_level":987.7,"humidity":81,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":64},"wind":{"speed":2.97,"deg":96.0021},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-06-27 21:00:00"},{"dt":1467072000,"main":{"temp":26.71,"temp_min":26.71,"temp_max":26.71,"pressure":988.06,"sea_level":1012.31,"grnd_level":988.06,"humidity":94,"temp_kf":0},"weather":[{"id":501,"main":"Rain","description":"moderate rain","icon":"10d"}],"clouds":{"all":76},"wind":{"speed":0.71,"deg":189.5},"rain":{"3h":3.205},"sys":{"pod":"d"},"dt_txt":"2016-06-28 00:00:00"},{"dt":1467082800,"main":{"temp":29.3,"temp_min":29.3,"temp_max":29.3,"pressure":990,"sea_level":1014.38,"grnd_level":990,"humidity":91,"temp_kf":0},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":{"all":80},"wind":{"speed":2.86,"deg":100.504},"rain":{"3h":0.005},"sys":{"pod":"d"},"dt_txt":"2016-06-28 03:00:00"}]}