JSON返回示例:
{ /*JSONObject*/
"resultcode": "200",
"reason": "SUCCESSED!",
"result": [ /*JSONArray*/
{ /*JSONObject*/
"city": "苏州", /*城市*/
"PM2.5": "73", /*PM2.5指数*/
"AQI": "98", /*空气质量指数*/
"quality": "良", /*空气质量*/
"PM10": "50",/*PM10*/
"CO": "0.79", /*一氧化碳*/
"NO2": "65", /*二氧化氮*/
"O3": "28", /*臭氧*/
"SO2": "41", /*二氧化硫*/
"time": "2014-12-26 11:48:40"/*更新时间*/
}
],
"error_code": 0
}
实例:JSONDemo
运行效果:
{ /*JSONObject*/
"resultcode": "200",
"reason": "SUCCESSED!",
"result": [ /*JSONArray*/
{ /*JSONObject*/
"city": "苏州", /*城市*/
"PM2.5": "73", /*PM2.5指数*/
"AQI": "98", /*空气质量指数*/
"quality": "良", /*空气质量*/
"PM10": "50",/*PM10*/
"CO": "0.79", /*一氧化碳*/
"NO2": "65", /*二氧化氮*/
"O3": "28", /*臭氧*/
"SO2": "41", /*二氧化硫*/
"time": "2014-12-26 11:48:40"/*更新时间*/
}
],
"error_code": 0
}
实例:JSONDemo
运行效果:
主要代码片段:
protected void onPostExecute(String result) {
if (result != null) {
try {
JSONObject jsonObject = new JSONObject(result);
int resultCode = jsonObject.getInt("resultcode");
if (resultCode == 200) {
JSONArray resultJsonArray = jsonObject.getJSONArray("result");
JSONObject resultJsonObject = resultJsonArray.getJSONObject(0);
String output = context.getString(R.string.city) + ": " + resultJsonObject.getString("city") + "\n"
+ context.getString(R.string.PM25) + ": " + resultJsonObject.getString("PM2.5") + "\n"
+ context.getString(R.string.AQI) + ": " + resultJsonObject.getString("AQI") + "\n"
+ context.getString(R.string.quality) + ": " + resultJsonObject.getString("quality") + "\n"
+ context.getString(R.string.PM10) + ": " + resultJsonObject.getString("PM10") + "\n"
+ context.getString(R.string.CO) + ": " + resultJsonObject.getString("CO") + "\n"
+ context.getString(R.string.NO2) + ": " + resultJsonObject.getString("NO2") + "\n"
+ context.getString(R.string.O3) + ": " + resultJsonObject.getString("O3") + "\n"
+ context.getString(R.string.SO2) + ": " + resultJsonObject.getString("SO2") + "\n"
+ context.getString(R.string.time) + ": " + resultJsonObject.getString("time") + "\n";
tv_result.setText(output);
} else if (resultCode == 202) {
String reason = jsonObject.getString("reason");
tv_result.setText(reason);
} else {
Toast.makeText(context, "查询失败",
Toast.LENGTH_LONG).show();
tv_result.setText("");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Toast.makeText(context, "查询失败",
Toast.LENGTH_LONG).show();
tv_result.setText("");
}
}