编辑:我从错误的对象那里得到了花车 - 我的坏。但是时间戳有同样的问题 - Int无法转换为JSONObject
我从openweathermap获得城市和国家的罚款,但应用程序崩溃浮动值。我是Android的新手,是我第一次使用JSON所以任何帮助将不胜感激。 我正在阅读7天的预测。这是我的WeatherParser类,它从文本视图传递到城市位置:
JSONObject jObj = new JSONObject(data);
JSONObject city = jObj.getJSONObject("city");
Location loc = new Location();
loc.setCity(getString("name", city));
loc.setCountry(getString("country", city));
weather.setLocation(loc);
JSONArray jArr = jObj.getJSONArray("list");
for (int i = 0; i < jArr.length(); i++) {
JSONObject dailyForecast = jArr.getJSONObject(i);
JSONObject time = dailyForecast.getJSONObject("dt");
JSONObject temp = dailyForecast.getJSONObject("temp");
JSONObject dayTemp = temp.getJSONObject("day");
JSONObject max = temp.getJSONObject("max");
JSONObject min = temp.getJSONObject("min");
DailyForecast df = new DailyForecast();
long unixSeconds = (long) getLong("dt", time);
Date date = new Date(unixSeconds*1000L); // *1000 is to convert minutes to milliseconds
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z", Locale.US);
String formattedDate = sdf.format(date);
df.setDay(formattedDate);
df.setTemp(getFloat("day", dayTemp));
df.setMax(getFloat("max", max));
df.setMin(getFloat("min", min));
JSONArray jAr = dailyForecast.getJSONArray("weather");
if(jAr.length() > 2) {
JSONObject desc = jAr.getJSONObject(2); //get the 3rd item
df.setDesc(getString("description", desc));
}
weeksForecast[i] = df;
}
weather.fillForecast(weeksForecast);
return weather;
}
private static String getString(String tagName, JSONObject jObj)
throws JSONException {
return jObj.getString(tagName);
}
private static float getFloat(String tagName, JSONObject jObj)
throws JSONException {
return (float) jObj.getDouble(tagName);
}
private static float getLong(String tagName, JSONObject jObj)
throws JSONException {
return (long) jObj.getDouble(tagName);
}
}
10-24 19:48:03.728:D / gralloc_goldfish(954):未检测到GPU仿真的仿真器。 10-24 19:48:12.398:D / dalvikvm(954):GC_FOR_ALLOC释放163K,9%空闲2855K / 3136K,暂停36ms,总计36ms 10-24 19:48:12.448:W / System.err(954):org.json.JSONException:类型为java.lang.Double的日期值为12.74无法转换为JSONObject 10-24 19:48:12.448:W / System.err(954):at org.json.JSON.typeMismatch(JSON.java:100) 10-24 19:48:12.448:W / System.err(954):at org.json.JSONObject.getJSONObject(JSONObject.java:577) 10-24 19:48:12.448:W / System.err(954):at com.meaProject.yawa.WeatherParser.getWeather(WeatherParser.java:31) 10-24 19:48:12.448:W / System.err(954):at com.meaProject.yawa.MainActivity $ JSONWeatherTask.doInBackground(MainActivity.java:58) 10-24 19:48:12.458:W / System.err(954):at com.meaProject.yawa.MainActivity $ JSONWeatherTask.doInBackground(MainActivity.java:1) 10-24 19:48:12.458:W / System.err(954):at android.os.AsyncTask $ 2.call(AsyncTask.java:287) 10-24 19:48:12.458:W / System.err(954):at java.util.concurrent.FutureTask.run(FutureTask.java:234) 10-24 19:48:12.458:W / System.err(954):at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230) 10-24 19:48:12.458:W / System.err(954):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 10-24 19:48:12.458:W / System.err(954):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:573) 10-24 19:48:12.468:W / System.err(954):at java.lang.Thread.run(Thread.java:841)