androidの获取天气预报JSON 实现
1. 首先推荐json格式检查工具
http://www.bejson.com/
有效查询该json 格式是否合法
2. JSON中两种结构:对象和数组
1)、对象:对象在js中表示为“{}”括起来的内容,数据结构为 {key:value,key:value,…} 的键值对的结构,在面向对象的语言中,key为对象的属性,value为对应的属性值,所以很容易理解,取值方法为 对象.key 获取属性值,这个属性值的类型可以是 数字、字符串、数组、对象几种。
2)、数组:数组在js中是中括号“[]”括起来的内容,数据结构为 [“java”,“javascript”,“vb”,…],取值方式和所有语言中一样,使用索引获取,字段值的类型可以是 数字、字符串、数组、对象几种。
(1)json对象格式1
{"name":"sam","age":18,"weight":60}
即为
{
"name": "sam",
"age": 18,
"weight": 60
}
解析方法:
JSONObject jsonObj;
try {
jsonObj = new JSONObject(str);
String name = jsonObj.optString("name");
int age = jsonObj.optInt("age");
int weight = jsonObj.optInt("weight");
} catch (JSONException e) {
e.printStackTrace();
(2) JSON对象格式2
{"weatherinfo":{"city":"北京","cityid":"101010100","temp":"9","WD":"西南风",
"WS":"2级","SD":"22%","WSE":"2","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB","njd":"暂无实况","qy":"1014","time":"10:45"}}
即为
{
"weatherinfo": {
"city": "北京",
"cityid": "101010100",
"temp": "9",
"WD": "西南风",
"WS": "2级",
"SD": "22%",
"WSE": "2",
"isRadar": "1",
"Radar": "JC_RADAR_AZ9010_JB",
"njd": "暂无实况",
"qy": "1014",
"time": "10:45"
}
}
解析方法:
JSONObject jsonObj;
try {
jsonObj = new JSONObject(str).getJSONObject("weatherinfo");
int tmp = jsonObj.optInt("temp");
String city=jsonObj.optString("city");
} catch (JSONException e) {
e.printStackTrace();
}
(3)JSON 数组格式
[12,13,15]
//json2 一个数字数组
解析方法
解析方法
JSONArray jsonArray;
try {
jsonArray = new JSONArray(str);
for (int i= 0; i < jsonArray.length();i++) {
int age = jsonArray.optInt(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
(4)JSON复杂数据格式
[{
"name"
:
"sam"
,
"age"
:18},{
"name"
:
"leo"
,
"age"
:19},{
"name"
:
"sky"
,
"age"
:20}]
即为
即为
[
{
"name": "sam",
"age": 18
},
{
"name": "leo",
"age": 19
},
{
"name": "sky",
"age": 20
}
]
解析方法:
JSONArray jsonArray;
try {
jsonArray = new JSONArray(str);
for (int i= 0; i < jsonArray.length();i++) {
JSONObject jsonObject = jsonArray.optJSONObject(i);
String name = jsonObject.optString("name");
int age = jsonObject.optInt("age");
}
} catch (JSONException e) {
e.printStackTrace();
}
获取当天天气状况利用json实现,如图
从网络端获取的json格式为:
{"city":"上海","date":"周一 02月02日 (实时:6℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/xiaoyu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png","temperature":"8 ~ 5℃","weather":"小雨","wind":"东北风微风"}
2. 具体代码实现
首先想到是利用线程执行网络访问,否则会出错的。
private void updateTodayWea() {
new Thread() {
@Override
public void run() {
super.run();
String str = Constants.server + ":8080/beastat/"+ "todaywea.jsp";
strjson = Httputils.connServerForResult(str);
if (strjson != null) {
Message message = new Message();
message.what = 0;
handler.sendMessage(message);
} else {
Toast.makeText(MainActivity.this, "返回为空", 1000).show();
}
}
}.start();
}
利用Handler 处理,发送消息过去。
但是,我们要获取url ,Httputils.connServerForResult(str); 返回json数据内容。
// 参数strUrl
public static String connServerForResult(String strUrl) {
// HttpGet对象
HttpGet httpRequest = new HttpGet(strUrl);
String strResult = "";
try {
// HttpClient对象
HttpClient httpClient = new DefaultHttpClient();
// 获得HttpResponse对象
HttpResponse httpResponse = httpClient.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 取得返回的数据
strResult = EntityUtils.toString(httpResponse.getEntity());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return strResult;
}
通过get 获取网络端数据,然后解析
以下是解析普通json数据过程。
// 普通Json数据解析
private void parseJson(String strResult) {
try {
JSONObject jsonObj = new JSONObject(strResult);
String city = jsonObj.getString("city");
String date = jsonObj.getString("date");
String temperature = jsonObj.getString("temperature");
String weather = jsonObj.getString("weather");
String wind = jsonObj.getString("wind");
textview.setText("城市:" + city + " 时间:" + date + "\n天气:"
+ weather + " 风:" + wind + " 温度:" + temperature);
} catch (JSONException e) {
System.out.println("Json parse error");
e.printStackTrace();
}
}