用百度天气接口的实现安卓天气预报(json数据)

代码地址:

http://download.csdn.net/detail/u012606241/7608515

 

 

具体思路:

1.写一个线程,其中city_str就是城市名,例如

 

city_str="宁波";


 

2.然后通过线程取百度天气接口json数据

 

网址是:http://api.map.baidu.com/telematics/v3/weather?location=宁波&output=json&ak=GuZriL3rkm1MUnyTyfsNGvTC

(注意不要用ie开这个网址,用谷歌、火狐那些应该都可以,我用的是火狐)

打开网址后,就可以看到那些json数据了。

 

3.对取到数据进行处理(在void doingData(String data)函数里进行处理),并根据这些数据设置自己的控件显示

 

线程代码如下:

 

 

class MyAsyncTask extends AsyncTask<String, Integer, String> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			// super.onPreExecute();
			
			}
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			String path = "";
			try {
				city_str = cityname.getText().toString();
				citys_strlength=city_str.length();
				city_str = java.net.URLEncoder.encode(city_str, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			path = "http://api.map.baidu.com/telematics/v3/weather?location="
					+ city_str + "&output=json&ak=GuZriL3rkm1MUnyTyfsNGvTC";
			// http://api.map.baidu.com/telematics/v3/weather?location=北京&output=json&ak=GuZriL3rkm1MUnyTyfsNGvTC
			String jsonString = HttpUtils.getJsonContent(path);

			return jsonString;// 作为result传递到onPostExecute
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			// super.onPostExecute(result);
			doingData(result);// 处理得到的数据
		}

	}

	
	public void doingData(String data) {
		System.out.println("百度天气接口数据在这"+data);
//在这里处理数据,并根据所得到的值设置控件的显示就ok了。	
}


 

4.加入代码后,在onCreate(Bundle savedInstanceState)函数中加入以下这句代码就ok了。

 

new MyAsyncTask().execute();


 

 结果数据部分:

 

在doingData(String data)部分将得到的数据打印出来,就可以在system.out.print中看到这样的数据:

 

{
    "error":0,
    "status":"success",
    "date":"2014-05-12",
    "results":[
        {
            "currentCity":"\u5b81\u6ce2",
            "weather_data":[
                {
                    "date":"\u5468\u4e00(\u4eca\u5929, \u5b9e\u65f6\uff1a19\u2103)",
                    "dayPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/day\/duoyun.png",
                    "nightPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/night\/duoyun.png",
                    "weather":"\u591a\u4e91",
                    "wind":"\u5fae\u98ce",
                    "temperature":"15\u2103"
                },
                {
                    "date":"\u5468\u4e8c",
                    "dayPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/day\/yin.png",
                    "nightPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/night\/zhenyu.png",
                    "weather":"\u9634\u8f6c\u9635\u96e8",
                    "wind":"\u5fae\u98ce",
                    "temperature":"26 ~ 19\u2103"
                },
                {
                    "date":"\u5468\u4e09",
                    "dayPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/day\/zhenyu.png",
                    "nightPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/night\/zhenyu.png",
                    "weather":"\u9635\u96e8",
                    "wind":"\u5fae\u98ce",
                    "temperature":"25 ~ 20\u2103"
                },
                {
                    "date":"\u5468\u56db",
                    "dayPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/day\/duoyun.png",
                    "nightPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/night\/duoyun.png",
                    "weather":"\u591a\u4e91",
                    "wind":"\u5fae\u98ce",
                    "temperature":"26 ~ 15\u2103"
                }
            ]
        }
    ]
}


 


 

将json数据拿去   【百度  json数据解析】  一下就可以看到这样的结果(可以根据解析结果进行数据分析):

 

 

 


 

 资源地址:http://download.csdn.net/detail/u012606241/7608515    (0积分的)


 

 

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
天气查询APP,两种JSON解析方式 /** * 原始json数据解析 * */ // JSONObject jsonObject = new JSONObject(res); // String reason=jsonObject.getString("reason"); // if (reason.equals("参数不正确")){ // handler.sendEmptyMessage(1); // return; // } // JSONObject result=jsonObject.getJSONObject("result"); // JSONObject realtime=result.getJSONObject("realtime"); // JSONObject life=result.getJSONObject("life"); // JSONObject wind=realtime.getJSONObject("wind"); // String time=realtime.getString("time"); // JSONObject weather=realtime.getJSONObject("weather"); // String date=realtime.getString("date"); // dateStr=time+date; // weekStr=realtime.getString("week"); // calendarStr=realtime.getString("moon"); // windpowerStr=wind.getString("direct")+" "+wind.getString("power"); // weatherStr=weather.getString("info"); // temperatureStr=weather.getString("temperature"); // JSONObject info=life.getJSONObject("info"); // JSONArray kongtiao=info.getJSONArray("kongtiao"); // JSONArray yundong=info.getJSONArray("yundong"); // JSONArray ziwaixian=info.getJSONArray("ziwaixian"); // ACStr=kongtiao.getString(0)+" "+kongtiao.getString(1); // sportStr=yundong.getString(0)+" "+yundong.getString(1); // lightStr=ziwaixian.getString(0)+" "+ziwaixian.getString(1); /** * Gson数据解析 */ WheatherBean wheatherBean=new Gson().fromJson(res,WheatherBean.class); String reason=wheatherBean.getReason(); if (reason.equals("参数不正确")){ handler.sendEmptyMessage(1); return; } WheatherBean.ResultBean resultBean=wheatherBean.getResult(); WheatherBean.ResultBean.RealtimeBean realtimeBean=resultBean.getRealtime(); WheatherBean.ResultBean.RealtimeBean.WindBean windBean=realtimeBean.getWind(); String time=realtimeBean.getTime(); WheatherBean.ResultBean.RealtimeBean.WeatherBean weatherBean=realtimeBean.getWeather(); String date=realtimeBean.getDate(); dateStr=time+date; weekStr=realtimeBean.getWeek(); calendarStr=realtimeBean.getMoon(); windpowerStr=windBean.getDirect()+" "+windBean.getPower(); temperatureStr=weatherBean.getTemperature(); weatherStr=weatherBean.getInfo(); WheatherBean.ResultBean.LifeBean lifeBean=resultBean.getLife(); WheatherBean.ResultBean.LifeBean.InfoBean infoBean=lifeBean.getInfo(); List<String> kongtiao=infoBean.getKongtiao(); List<String> yundong=infoBean.getYundong(); List<String> ziwaixian=infoBean.getZiwaixian(); ACStr=kongtiao.get(0)+" "+kongtiao.get(1); sportStr=yundong.get(0)+" "+yundong.get(1); lightStr=ziwaixian.get(0)+" "+ziwaixian.get(1); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值