我已经编写了一个显示json天气数据的程序但是,我在logcat中收到了响应,但是我的listview没有显示,它说org.JSOn.JSONException:没有天气值
网址:http://api.openweathermap.org/data/2.5/forecast/daily?q=Mumbai&mode=json&units=metric&cnt=7
MainActivity.java:
public class MainActivity extends Activity {
ListView lv;
// URL to get contacts JSON
private static String url =
"http://api.openweathermap.org/data/2.5/forecast/daily?
q=Mumbai&mode=json&units=metric&cnt=7";
// JSON Node names
static final String TAG_LIST = "list";
static final String TAG_DATE = "dt";
static final String TAG_TEMP = "temp";
static final String TAG_DAY = "day";
static final String TAG_NIGHT = "night";
static final String TAG_MIN = "min";
static final String TAG_MAX = "max";
static final String TAG_MORN = "morn";
static final String TAG_EVE = "eve";
static final String TAG_PRESSURE = "pressure";
static final String TAG_HUMIDITY = "humidity";
static final String TAG_WEATHER = "weather";
static final String TAG_MAIN = "main";
static final String TAG_DESCRIPTION = "description";
static final String TAG_ICON = "icon";
JSONArray list = null;
JSONArray weatherList = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> weatherArrayList;
ProgressDialog pDialog;
ListViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listForecast);
weatherArrayList = new ArrayList<HashMap<String, String>>();
// ForecastAdapter adapter = new ForecastAdapter(getBaseContext(),
// forecastArray);
// lv.setAdapter(adapter);
// Calling async task to get json
new GetWeatherDetails().execute();
}
class GetWeatherDetails extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
list = jsonObj.getJSONArray(TAG_LIST);
for (int i = 0; i < list.length(); i++) {
JSONObject c = list.getJSONObject(i);
String date = c.getString(TAG_DATE);
JSONObject temp = c.getJSONObject(TAG_TEMP);
String day = temp.getString(TAG_DAY);
String night = temp.getString(TAG_NIGHT);
String min = temp.getString(TAG_MIN);
String max = temp.getString(TAG_MAX);
String morn = temp.getString(TAG_MORN);
String eve = temp.getString(TAG_EVE);
String pressure = c.getString(TAG_PRESSURE);
String humidity = c.getString(TAG_HUMIDITY);
weatherList = jsonObj.getJSONArray(TAG_WEATHER);
for (int j = 0; j < weatherList.length(); j++) {
JSONObject d = weatherList.getJSONObject(j);
String main = d.getString(TAG_MAIN);
String desc = d.getString(TAG_DESCRIPTION);
String icon = d.getString(TAG_ICON);
// tmp hashmap for single contact
HashMap<String, String> weatherMap = new
HashMap<String, String>();
// adding each child node to HashMap key => value
weatherMap.put(TAG_DATE, date);
weatherMap.put(TAG_DAY, day);
weatherMap.put(TAG_NIGHT, night);
weatherMap.put(TAG_MIN, min);
weatherMap.put(TAG_MAX, max);
weatherMap.put(TAG_MORN, morn);
weatherMap.put(TAG_EVE, eve);
weatherMap.put(TAG_PRESSURE, pressure);
weatherMap.put(TAG_HUMIDITY, humidity);
weatherMap.put(TAG_MAIN, main);
weatherMap.put(TAG_DESCRIPTION, desc);
weatherMap.put(TAG_ICON, icon);
// adding contact to contact list
weatherArrayList.add(weatherMap);
}
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this, weatherArrayList);
// Set the adapter to the ListView
lv.setAdapter(adapter);
}
}
Logcat:
05-21 12:39:12.174:
D/Response:(17460): > {"cod":"200","message":0.0092,"city":{"id":1275339,"name":"Mumbai","coord":{"lon":72.847939,"lat":19.01441},"country":"IN","population":0,"sys":{"population":0}},"cnt":7,"list":[{"dt":1432191600,"temp":{"day":33.16,"min":24.81,"max":35.41,"night":24.81,"eve":34.14,"morn":33.16},"pressure":1006.17,"humidity":63,"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"speed":2.18,"deg":266,"clouds":0},{"dt":1432278000,"temp":{"day":32.81,"min":25.66,"max":35.27,"night":25.66,"eve":33.91,"morn":26.1},"pressure":1006.21,"humidity":66,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"speed":2.08,"deg":274,"clouds":0},{"dt":1432364400,"temp":{"day":34.16,"min":25.66,"max":35.6,"night":25.66,"eve":34.1,"morn":27.03},"pressure":1005.64,"humidity":59,"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"speed":3.52,"deg":288,"clouds":0},{"dt":1432450800,"temp":{"day":34.63,"min":25.52,"max":35.72,"night":25.52,"eve":34.3,"morn":26.46},"pressure":1006.31,"humidity":55,"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"speed":3.67,"deg":280,"clouds":0},{"dt":1432537200,"temp":{"day":33.57,"min":24.25,"max":33.57,"night":27.11,"eve":33.03,"morn":24.25},"pressure":1003.67,"humidity":0,"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"speed":3.48,"deg":322,"clouds":0},{"dt":1432623600,"temp":{"day":33.56,"min":24.81,"max":33.56,"night":27.5,"eve":33.35,"morn":24.81},"pressure":1004.19,"humidity":0,"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"speed":3.32,"deg":334,"clouds":0},{"dt":1432710000,"temp":{"day":34.36,"min":25.34,"max":34.36,"night":28.31,"eve":33.89,"morn":25.34},"pressure":1004.14,"humidity":0,"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"speed":4.05,"deg":331,"clouds":0}]}
05-21 12:39:12.244: W/System.err(17460): org.json.JSONException: No value for weather
05-21 12:39:12.259: W/System.err(17460): at org.json.JSONObject.get(JSONObject.java:354)
05-21 12:39:12.259: W/System.err(17460): at org.json.JSONObject.getJSONArray(JSONObject.java:544)
05-21 12:39:12.259: W/System.err(17460): at com.example.sunshine.MainActivity$GetWeatherDetails.doInBackground(MainActivity.java:118)
05-21 12:39:12.264: W/System.err(17460): at com.example.sunshine.MainActivity$GetWeatherDetails.doInBackground(MainActivity.java:1)
05-21 12:39:12.264: W/System.err(17460): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-21 12:39:12.264: W/System.err(17460): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-21 12:39:12.269: W/System.err(17460): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-21 12:39:12.279: W/System.err(17460): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-21 12:39:12.279: W/System.err(17460): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-21 12:39:12.279: W/System.err(17460): at java.lang.Thread.run(Thread.java:864)
答案 0 :(得分:2)
weather
是list
的直接子项,因此您应该列出以检索它,
weatherList = c.getJSONArray(TAG_WEATHER);
应该这样做