Android天气预报

每天从网上去下天气信息,显示出来。效果图如下:

这是连接网络的代码:

public class WebConnect {
	private Context context;
	public WebConnect(Context context){
		this.context=context;
	}
	public String ConnectWeb(String url){
		HttpGet request = new HttpGet(url);
		HttpParams params=new BasicHttpParams();
		HttpClient httpClient = new DefaultHttpClient(params);
		try{
			HttpResponse response = httpClient.execute(request);
			if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				String content = EntityUtils.toString(response.getEntity());
				return content;
			} else {
				Toast.makeText(context, "网络访问失败,请检查您机器的联网设备!", Toast.LENGTH_LONG).show();
			}
			
		}catch(Exception e) {
			e.printStackTrace();
		} finally {
			httpClient.getConnectionManager().shutdown();
		}
		return null;
	}

}
第一次启动时:

private void firstRunAPP() {
		copyDatabase();
		String url = "http://m.weather.com.cn/data/101010100.html";
		String info = new WebConnect(this).ConnectWeb(url);
		if(info==null){
			return;
		}
		setWeather(info, 1);
		serchWeather(info);
	}

copy城市信息copyDatabase():

private void copyDatabase() {
		String dirPath = "/data/data/com.weather.jht/databases";
		File dir = new File(dirPath);
		if (!dir.exists()) {
			dir.mkdir();
		}
		File dbfile = new File(dir, "db_weather.db");
		try {
			if (!dbfile.exists()) {
				dbfile.createNewFile();
			}
			InputStream is = this.getApplicationContext().getResources()
					.openRawResource(R.raw.db_weather);
			FileOutputStream fos = new FileOutputStream(dbfile);
			byte[] buffere = new byte[is.available()];
			is.read(buffere);
			fos.write(buffere);
			is.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

默认显示北京的天气
String url = "http://m.weather.com.cn/data/101010100.html";
101010100是北京的城市ID。以后每次启动,判断天气信息是不是当天的,如果是,直接从sharedpreference里读取数据,如果不是,重新去网上取新的天气信息。

                                long daySpan = 24 * 60 * 60 * 1000;
				String date = preference.getString(Constant.DateTime, "");
				Date NowDate = new Date();
				long nowtime=NowDate.getTime();
				SimpleDateFormat DateFormat = new SimpleDateFormat(
						Constant.Dateformat);
				Date SherdDate = DateFormat.parse(date);
				long sherdtime=SherdDate.getTime();
				if (nowtime > sherdtime+daySpan) {
					String url = "http://m.weather.com.cn/data/" + cityCode
							+ ".html";
					String info = new WebConnect(this).ConnectWeb(url);
					setWeather(info, 3);
					serchWeather(info);
				} else {
					setWeatherByShared(preference);
				}
这个info传进serchWeather()里

JSONObject json = new JSONObject(info).getJSONObject("weatherinfo");

weatherinfo的信息如下:

{"weatherinfo": {
//基本信息;
"city":"北京","city_en":"beijing",
"date_y":"2012年2月16日", "date":"", "week":"星期四", "fchh":"11", "cityid":"101010100",
//摄氏温度
"temp1":"2℃~-7℃",
"temp2":"1℃~-7℃",
"temp3":"4℃~-7℃",
"temp4":"7℃~-5℃",
"temp5":"5℃~-3℃",
"temp6":"5℃~-2℃",
//华氏温度;
"tempF1":"35.6℉~19.4℉",
"tempF2":"33.8℉~19.4℉",
"tempF3":"39.2℉~19.4℉",
"tempF4":"44.6℉~23℉",
"tempF5":"41℉~26.6℉",
"tempF6":"41℉~28.4℉",
//天气描述;
"weather1":"晴",
"weather2":"晴",
"weather3":"晴",
"weather4":"晴转多云",
"weather5":"多云",
"weather6":"多云转阴",
//天气描述图片序号
"img1":"0",
"img2":"99",
"img3":"0",
"img4":"99",
"img5":"0",
"img6":"99",
"img7":"0",
"img8":"1",
"img9":"1",
"img10":"99",
"img11":"1",
"img12":"2",
"img_single":"0",
//图片名称;
"img_title1":"晴",
"img_title2":"晴",
"img_title3":"晴",
"img_title4":"晴",
"img_title5":"晴",
"img_title6":"晴",
"img_title7":"晴",
"img_title8":"多云",
"img_title9":"多云",
"img_title10":"多云",
"img_title11":"多云",
"img_title12":"阴",
"img_title_single":"晴",
//风速描述
"wind1":"北风3-4级转微风",
"wind2":"微风",
"wind3":"微风",
"wind4":"微风",
"wind5":"微风",
"wind6":"微风",
//风速级别描述
"fx1":"北风",
"fx2":"微风",
"fl1":"3-4级转小于3级",
"fl2":"小于3级",
"fl3":"小于3级",
"fl4":"小于3级",
"fl5":"小于3级",
"fl6":"小于3级",
//今天穿衣指数;
"index":"冷",
"index_d":"天气冷,建议着棉衣、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣或冬大衣。",
//48小时穿衣指数
"index48":"冷",
"index48_d":"天气冷,建议着棉衣、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣或冬大衣。",
//紫外线及48小时紫外线
"index_uv":"弱",
"index48_uv":"弱",
//洗车
"index_xc":"适宜",
//旅游
"index_tr":"一般",
//舒适指数
"index_co":"较不舒适",


"st1":"1",
"st2":"-8",
"st3":"2",
"st4":"-4",
"st5":"5",
"st6":"-5",
//晨练
"index_cl":"较不宜",
//晾晒
"index_ls":"基本适宜",
//过敏
"index_ag":"极不易发"}}

自己可以根据需求取相应的数据

String today = json.getString("date_y")这就是取的哪天的数据。

天气预报源码


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值