我写了一个程序来显示json天气数据,但是,我在我的logcat中收到响应,但我的listview没有显示,它说org.JSOn.JSONException:天气没有价值
网址 : http://api.openweathermap.org/data/2.5/forecast/daily?q=Mumbai
主要活动.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)
天气
是列表的直接子级
,所以你应该列出来检索它,
weatherList = c.getJSONArray(TAG_WEATHER);
应该这么做
当我运行代码时,我得到以下错误。我正在使用SeleniumGrid在远程服务器上运行代码。尽管当我在本地机器上运行时,代码运行良好。 错误 ----------- 系统 Windows server R2数据中心/ 64位操作系统sikuli 1.0.2
抬手即可在天气手表表盘中查看当天的每小时预报以及明天的 3 小时预报和后天的 6 小时预报。提供的其他天气信息包括风速、风向、湿度和降水概率。 只能在天气手表表盘中查看天气信息。在时间视图中,向左或者向右滑动以找到天气信息。 要使用天气功能,您需要在手机上安装 Flow 应用程序并与手表配对。您还需要开启定位服务 (iOS) 或定位设置 (Android) 才能获取天气信息。 今日预报 预报位置
抬手即可在天气手表表盘中查看当天的每小时预报以及明天的 3 小时预报和后天的 6 小时预报。提供的其他天气信息包括风速、风向、湿度和降水概率。 只能在天气手表表盘中查看天气信息。在时间视图中,向左或者向右滑动以找到天气信息。 要使用天气功能,您需要在手机上安装 Flow 应用程序并与手表配对。您还需要开启定位服务 (iOS) 或定位设置 (Android) 才能获取天气信息。 今日预报 预报位置
简单的天气预报Demo。用到json解析,asihttp请求,使用中国天气网的接口。供初学者参考。 [Code4App.com]
卤面网制作的开源卤天气软件,最终解释权归卤面网及作者所有。同时欢迎转载,转载请保留此声明。
不允许泛型类直接或间接扩展Throwable类。 //The generic class Box<T> may not subclass java.lang.Throwable class Box<T> extends Exception {} //The generic class Box<T> may not subclass java.lang.Throwable class Box1<T>