空空天气预报

之前写了一款基于Android的空空天气预报,能够查看全国各个省、市、县的未来三天的天气预报,有穿衣指数和运动指数建议;

最近准备找工作了,现在重新回顾一下思路。

主要用到的知识有解析xml技术,解析json,mvc架构。

1、首先看下界面结果;

2、代码思路

2-1 代码架构图

2-2 设计思路

2-2-1 首先解析一个省市县xml文件,里面的代码样式如图:

  <province id="01" name="北京">
    <city id="0101" name="北京">
      <county id="010101" name="北京" weatherCode="101010100"/>
      <county id="010102" name="海淀" weatherCode="101010200"/>
      <county id="010103" name="朝阳" weatherCode="101010300"/>
      <county id="010104" name="顺义" weatherCode="101010400"/>
      <county id="010105" name="怀柔" weatherCode="101010500"/>
      <county id="010106" name="通州" weatherCode="101010600"/>
      <county id="010107" name="昌平" weatherCode="101010700"/>
      <county id="010108" name="延庆" weatherCode="101010800"/>
      <county id="010109" name="丰台" weatherCode="101010900"/>
      <county id="010110" name="石景山" weatherCode="101011000"/>
      <county id="010111" name="大兴" weatherCode="101011100"/>
      <county id="010112" name="房山" weatherCode="101011200"/>
      <county id="010113" name="密云" weatherCode="101011300"/>
      <county id="010114" name="门头沟" weatherCode="101011400"/>
      <county id="010115" name="平谷" weatherCode="101011500"/>
    </city>
  </province>

2-2-2 模型层的建立

主要是在model层里面建立City、County、Province三个类,下面贴一部分代码:

public class County {
    private int id;//县城主id
    private String countyName;//县城名
    private String countyCode;//县城代码
    private int cityId;//城市id
    ...
public class City {
    private int id;//城市id
    private String cityName;//城市名称
    private String cityCode;//城市代码
    private int provinceId;//省id
        ...
public class Province {
    private int id;//省id
    private String provinceName;//省名
    private String provinceCode;//省代码
    public int getId() {
        ...

2-2-3 数据库的设计

设计两个类,一个类是CoolWeatherOpenHelper 继承自 SQLiteOpenHelper,用于数据库的创建和更新,大致代码如下:

public class CoolWeatherOpenHelper extends SQLiteOpenHelper {

    public CoolWeatherOpenHelper(Context context, String name,CursorFactory factory, int version) {
        super(context, name, factory, version);
    }
    //创建省、市、县的sql语句
    public static final String CREATE_PROVINCE =
                  "create table Province ("
                + "id integer primary key autoincrement,"
                + "provinceName text,"
                + "provinceCode text)";
    public static final String CREATE_CITY =
                  "create table City ("
                + "id integer primary key autoincrement,"
                + "cityName text,"
                + "cityCode text,"
                + "provinceId integer)";
    public static final String CREATE_COUNTY =
                  "create table County ("
                + "id integer primary key autoincrement,"
                + "countyName text,"
                + "countyCode text,"
                + "cityId integer)";
    //创建数据库
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_PROVINCE); //create tables
        db.execSQL(CREATE_CITY);
        db.execSQL(CREATE_COUNTY);
    }
...

设计第二个类CoolWeatherDB,用于插入xml文件中省市县的天气预报代码

public class CoolWeatherDB {
	/**
	 * database name 数据库名
	 */
	public static final String DB_NAME = "cool_weather.db";
	/**
	 * database version ,database is used for operating the database (insert , update, delete, select)
	 */
	public static final int VERSION = 1;
	private static CoolWeatherDB coolWeatherDB;
	private SQLiteDatabase db;

	/**
	 * constructor构造函数
	 */
	private CoolWeatherDB(Context context) {
		CoolWeatherOpenHelper dbHelper = new CoolWeatherOpenHelper(context, DB_NAME, null, VERSION);
		db = dbHelper.getWritableDatabase();//call onCreate method
	}
	/**
	 * get the instance of CoolWeatherDB, use the singleInstance modern
	 * 获得CoolWeatherDB对象,这里使用了单例模式
	 */
	public  synchronized static CoolWeatherDB getInstance(Context context) {
		if(coolWeatherDB == null)
			coolWeatherDB = new CoolWeatherDB(context);
		return coolWeatherDB;
	}
	/**
	 * insert province information to the database
	 * 插入省信息到数据库中
	 */
	public void saveProvince(Province province) {
		if(province != null) {
			ContentValues values = new ContentValues();
			values.put("provinceName", province.getProvinceName());
			values.put("provinceCode", province.getProvinceCode());
			db.insert("Province", null, values);
		}
	}
	/**
	 * insert a list of provinces
	 * 插入一系列省信息到数据库中
	 * @param provinces
	 */
	public void saveMangProvinces(ArrayList<Province> provinces) {
		if(provinces.size() > 0) {
			for(Province province: provinces) {
				ContentValues values = new ContentValues();
				values.put("provinceName", province.getProvinceName());
				values.put("provinceCode", province.getProvinceCode());
				db.insert("Province", null, values);
			}
		}
	}
	/**
	 * save city
	 * 插入城市信息到数据库中
	 */
	public void saveCity(City city) {
		if(city != null) {
			ContentValues values = new ContentValues();
			values.put("cityName", city.getCityName());
			values.put("cityCode", city.getCityCode());
			values.put("provinceId", city.getProvinceId());
			db.insert("City", null, values);
		}
	}
	/**
	 * insert a list of cities
	 * 插入一系列的城市信息到数据库中
	 * @param cities
	 */
	public void saveMangCities(ArrayList<City> cities) {

		if(cities.size() > 0) {
			for(City city: cities) {
				ContentValues values = new ContentValues();
				values.put("cityName", city.getCityName());
				values.put("cityCode", city.getCityCode());
				values.put("provinceId", city.getProvinceId());
				db.insert("City", null, values);
			}
		}
	}
	/**
	 * save county
	 * 插入县信息到数据库中
	 */
	public void saveCounty(County county) {
		if(county != null) {
			ContentValues values = new ContentValues();
			values.put("countyName", county.getCountyName());
			values.put("countyCode", county.getCountyCode());
			values.put("cityId", county.getCityId());
			db.insert("County", null, values);
		}
	}
	/**
	 * 插入一系列县信息到数据库中
	 * @param counties
	 */
	public void saveMangCounties(ArrayList<County> counties) {
		if(counties.size() > 0) {
			for(County county: counties) {
				ContentValues values = new ContentValues();
				values.put("countyName", county.getCountyName());
				values.put("countyCode", county.getCountyCode());
				values.put("cityId", county.getCityId());
				db.insert("County", null, values);
			}
		}
	}
	/**
	 * get province infor from the db
	 * 从数据库中获取省信息
	 */
	public List<Province> loadProvinces() {
		List<Province> list = new ArrayList<Province>();
		Cursor cursor = db.query("Province", null, null, null, null, null, null);
		if(cursor.moveToFirst()) {
			do{
				Province province = new Province();
				province.setId(cursor.getInt(cursor.getColumnIndex("id")));
				province.setProvinceName(cursor.getString(cursor.getColumnIndex("provinceName")));
				province.setProvinceCode(cursor.getString(cursor.getColumnIndex("provinceCode")));
				list.add(province);
			}while(cursor.moveToNext());
		}
		return list;
	}
	/**
	 * get city infor from the db by provinceId
	 * 根据省id从数据库中获取城市信息
	 */
	public List<City> loadCities(int provinceId) {
		List<City> list = new ArrayList<City>();
		Cursor cursor = db.query("City", null, "provinceId=?", new String[] {String.valueOf(provinceId)}, null, null, null);
		if(cursor.moveToFirst()) {
			do{
				City city= new City();
				city.setId(cursor.getInt(cursor.getColumnIndex("id")));
				city.setCityName(cursor.getString(cursor.getColumnIndex("cityName")));
				city.setCityCode(cursor.getString(cursor.getColumnIndex("cityCode")));
				list.add(city);
			}while(cursor.moveToNext());
		}
		return list;
	}
	/**
	 * get county information from the database by cityId
	 * 根据城市id从数据库中获取县城信息
	 */
	public List<County> loadCounties(String cityCode) {
		List<County> list = new ArrayList<County>();
		Cursor cursor = db.query("County", null, "cityId=?", new String[]{String.valueOf(cityCode)}, null, null, null);
		if(cursor.moveToFirst()) {
			do{
				County county = new County();
				county.setId(cursor.getInt(cursor.getColumnIndex("id")));
				county.setCountyName(cursor.getString(cursor.getColumnIndex("countyName")));
				county.setCountyCode(cursor.getString(cursor.getColumnIndex("countyCode")));
				county.setCityId(Integer.parseInt(cityCode));
				list.add(county);
			}while(cursor.moveToNext());
		}
		return list;
	}
}

2-2-4 设计解析xml工具类ParseCity

//用于解析xml中的省市县及对应的代码
public class ParseCity extends Thread {
	public static void parse(final InputStream in,final CoolWeatherDB coolWeatherDB) {
		new Thread(new Runnable() {
			ArrayList<Province> provinces = new ArrayList<Province>();// ArrayList存储多个省
			ArrayList<City> cities = new ArrayList<City>();// ArrayList存储多个市
			ArrayList<County> counties = new ArrayList<County>();// ArrayList存储多个县
			@Override
			public void run() {
				try {
					// 用XmlPullParserFactory去解析xml文件,获取实例
					XmlPullParserFactory factory = XmlPullParserFactory
							.newInstance();
					// 获取XmlPullParser类型的实例
					XmlPullParser xmlPullParser = factory.newPullParser();
					// 设置输入流和编码类型
					xmlPullParser.setInput(in, "utf-8");// set stream
					// 获取事件的类型
					int eventType = xmlPullParser.getEventType();
					// 获得的是事件类型,常用的有:XmlPullParser.END_DOCUMENT(xml文档结束)
					// XmlPullParser.START_DOCUMENT(文档开始),XmlPullParser.START_TAG(开始标签)
					// XmlPullParser.END_TAG(结束标签).XmlPullParser.TEXT(内容)
					String cityid = "";// 城市的id
					String fid = "";//设置外键的值
					// 使用while循环,解析完整个xml文件
					while (eventType != XmlPullParser.END_DOCUMENT) {
						String nodeName = xmlPullParser.getName();
						switch (eventType) {
						// begin to parse somenode 开始解析某个结点
						case XmlPullParser.START_DOCUMENT:
							break;
						case XmlPullParser.START_TAG: {
							//获取xml中的标记名称,有Province、City、County这三种
							String tagName = xmlPullParser.getName();
							//如果是省的话
							if ("province".equals(tagName)) {
								// get the count of province
								int count = xmlPullParser.getAttributeCount();
								String provinceId = null;//省id
								String provinceName = null;//省名称
								for (int i = 0; i < count; i++) {
									// 获取id
								String attrName = xmlPullParser.getAttributeName(i);
									// 获取省名称 北京
								String attrValue = xmlPullParser.getAttributeValue(i);
									if ("id".equals(attrName)) {
										// 用于设置在城市中的外键
										fid = attrValue;
										provinceId = attrValue;
									} else if ("name".equals(attrName)) {
										provinceName = attrValue;
									}
								}
								Province province = new Province();
								province.setProvinceCode(provinceId);
								province.setProvinceName(provinceName);
								// put the parsed data into the list provinces
								//保存省信息到数据库中
								coolWeatherDB.saveProvince(province);
							}
							//如果是城市
							if ("city".equals(tagName)) {
								//获取城市的数量
								int count = xmlPullParser.getAttributeCount();
								//城市名称
								String cityName = null;
								//城市代码
								String cityCode = null;
								//城市的外键,省id
								int provinceId = Integer.parseInt(fid);
								for (int i = 0; i < count; i++) {
								String attrName = xmlPullParser.getAttributeName(i);
									String attrValue = xmlPullParser
											.getAttributeValue(i);
									if ("id".equals(attrName)) {
										cityid = attrValue;// set foreign id in
										cityCode = attrValue;//城市代码
									} else if ("name".equals(attrName)) {
										cityName = attrValue;//城市名称
									}
								}
								City city = new City();
								city.setCityCode(cityCode);
								city.setCityName(cityName);
								city.setProvinceId(provinceId);
								//在数据库中保存城市
								coolWeatherDB.saveCity(city);
								// cities.add(city);
							}
							if ("county".equals(nodeName)) {
								int count = xmlPullParser.getAttributeCount();
								String countyName = null;//县名称
								String countyCode = null;//县代号
								int cityId = Integer.parseInt(cityid);//获得城市id
								for (int i = 0; i < count; i++) {
								String attrName = xmlPullParser.getAttributeName(i);
								String attrValue = xmlPullParser.getAttributeValue(i);
									if ("id".equals(attrName)) {
									} else if ("name".equals(attrName)) {
										countyName = attrValue;
									} else if ("weatherCode".equals(attrName)) {
										countyCode = attrValue;
									}
								}
								County county = new County();
								county.setCountyCode(countyCode);
								county.setCountyName(countyName);
								county.setCityId(cityId);
								//保存县值
								coolWeatherDB.saveCounty(county);
								// counties.add(county);
							}
							break;
						}
						case XmlPullParser.END_TAG:
							break;
						case XmlPullParser.END_DOCUMENT:
							/*
							 * coolWeatherDB.saveMangProvinces(provinces);
							 * coolWeatherDB.saveMangCities(cities);
							 * coolWeatherDB.saveMangCounties(counties);
							 */
							break;
						default:
							break;
						}
						eventType = xmlPullParser.next();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();

	}
}

2-2-5 选择城市界面

2-2-5-1 首先判断数据库中的内容是否为空,如果为空,那么就去调用ParseCity.parse(open,coolWeatherDB)的方法,如下

              try {
			//如果数据库为空,那么从xml文件中获得城市代号数据
			if(coolWeatherDB.loadProvinces().isEmpty()) {//get infor from xml
				showProgressDialog();//显示正在加载
				InputStream open = this.getResources().getAssets().open("city.xml");
				//调用parseCity
				ParseCity.parse(open,coolWeatherDB);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

2-2-5-2 获取数据后,默认显示省级别的城市,当点击其中一个城市后,在改ListView里面的点击事件去处理对应的逻辑,比如点击四川省,就去查询四川省下的城市,点击成都,就去查询成都下面的城市。

 listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int index,long arg3) {
				if(currentLevel == LEVEL_RPOVINCE) {
					selectedProvince = provinceList.get(index);
					queryCities();
				}else if(currentLevel == LEVEL_CITY) {
					selectedCity = cityList.get(index);
					queryCounties();
				}else if(currentLevel == LEVEL_COUNTY) {
					String countyCode = countyList.get(index).getCountyCode();                                        //跳转到天气查询界面
	                                Intent intent = new  Intent(ChooseAreaActivity.this,WeatherActivity.class);
					intent.putExtra("county_code", countyCode);
					startActivity(intent);
					finish();
				}
			}
		});下面举一个查询的例子,比如查询省下面的城市,调用queryCities()方法:
	private void queryCities() {
		cityList = coolWeatherDB.loadCities(selectedProvince.getId());//根据省id查询城市列表
		if(cityList.size() > 0){
			dataList.clear();
			for(City city:cityList) {
				dataList.add(city.getCityName());
			}
			adapter.notifyDataSetChanged();
			listView.setSelection(0);
			titleText.setText(selectedProvince.getProvinceName());
			currentLevel = LEVEL_CITY;
		}
	}

 2-2-5-3 如果查询到了最后一个级别,那么跳转到WeatherActivity界面,传递城市代号过去。

String countyCode = countyList.get(index).getCountyCode();
//跳转到天气查询界面
Intent intent = new  Intent(ChooseAreaActivity.this,WeatherActivity.class);
intent.putExtra("county_code", countyCode);
startActivity(intent);
finish();

 2-2-5-4 WeatherActivity界面代码解析

//获取从ChooseAreaActitivty界面传过来的参数值
String countyCode = getIntent().getStringExtra("county_code");
           if(!TextUtils.isEmpty(countyCode)) {
			data_y.setText("同步中...");
			weatherInfoLayout.setVisibility(View.INVISIBLE);
			cityNameText.setVisibility(View.INVISIBLE);
			//查询天气信息
			queryWeatherCode(countyCode);
			//queryWeatherInfor(countyCode);
			//new ConnectNetThread(WeatherActivity.this, countyCode).start();
		}
private void queryWeatherCode(String countyCode) {
		//查询天气预报网址
		String address = "http://weatherapi.market.xiaomi.com/wtr-v2/weather?cityId=" + countyCode;
		//查询
		queryFromServer(address,"weatherCode");
	}
private void queryFromServer(final String address,final String type) {
		HttpUtil.sendHttpRequest(address,
			new HttpCallbackListener() {
			@Override
			public void onFinish(String response) {
				if("weatherCode".equals(type)) {
					//回调处理response字符串,即处理天气预报返回的结果
					Utility.hadnleWeatherResponse(WeatherActivity.this, response);
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							//显示天气预报信息
							showWeather();
						}
					});
				}
			}

			@Override
			public void onError(Exception e) {
				// TODO Auto-generated method stub
				runOnUiThread(new Runnable() {

					@Override
					public void run() {
						data_y.setText("同步失败");
					}
				});
			}

		});
	}

2-2-5-5 然后去HttpUtil.sendHttpRequest()方法中通过连接网络从服务器端获取json数据,回调HttpCallbackListener接口

其中HttpCallbackListener是一个接口

public interface HttpCallbackListener {
	void onFinish(String response);//调用完成时的方法
	void onError(Exception e);//出现异常时调用的
}

HttpUtil.sendHttpRequest()的具体实现为:

public static void sendHttpRequest(final String address,final HttpCallbackListener listener) {
		//create a new thread to execute the method
	  new Thread(new Runnable() {
		@Override
		public void run() {
			HttpURLConnection connection = null;
			try{
				URL url = new URL(address);//初始化url对象
				//通过url得到HttpURLConnection对象
				connection = (HttpURLConnection) url.openConnection();
				connection.setRequestMethod("GET");
				connection.setConnectTimeout(8000);
				connection.setReadTimeout(8000);
				//获取输入字节流
				InputStream in = connection.getInputStream();//get inputStream
				//将输入字节流转换为带缓冲的输入字符流
				BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
				StringBuilder response = new StringBuilder();
				String line;
				while((line=reader.readLine()) != null) {
					response.append(line);
				}//return response
				in.close();
				if(listener != null) {
					// call back HttpCallbackListener interface: onFinish() method
					listener.onFinish(response.toString());
				}
			}catch(Exception e) {
				if(listener != null) {
					//call back HttpCallbackListener interface:  onError()  method
					listener.onError(e);
				}
			}finally{
				if(connection != null) {
					connection.disconnect();
				}
			}
		 }
		}).start();
	}

HttpUtil.sendHttpRequest()方法执行完后,调用

listener.onFinish(response.toString());

然后返回到WeatherActivity中的queryFromServer方法中

HttpUtil.sendHttpRequest(address,
			new HttpCallbackListener() {
			@Override
			public void onFinish(String response) {
				if("weatherCode".equals(type)) {
					//回调处理response字符串,即处理天气预报返回的结果
					Utility.hadnleWeatherResponse(WeatherActivity.this, response);
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							//显示天气预报信息
							showWeather();
						}
					});
				}
			}

接着执行Utility.hadnleWeatherResponse()的方法

public static void hadnleWeatherResponse(Context context,String response) {
		try {
			//新建JSONObject对象
			JSONObject jsonObject = new JSONObject(response);
			//获取天气预报json对象
			JSONObject weatherInfor = jsonObject.getJSONObject("forecast");
			//获取json数组
			JSONArray jsonArr = jsonObject.getJSONArray("index");
			//int iSize = jsonArr.length();
			//获取运动指数
			JSONObject jsonSports = jsonArr.getJSONObject(1);
			//获取穿衣指数
			JSONObject jsonWear = jsonArr.getJSONObject(2);
			String cityName = weatherInfor.getString("city");
			String cityid = weatherInfor.getString("cityid");//not show
			String date_y = weatherInfor.getString("date_y");
			String wind1 = weatherInfor.getString("wind1");
String weather_desp1 = "今日  " + weatherInfor.getString("temp1") + "  " + weatherInfor.getString("weather1");
String weather_desp2 = "明日  " + weatherInfor.getString("temp2") + "  " + weatherInfor.getString("weather2");
String weather_desp3 = "后日  " + weatherInfor.getString("temp3") + "  " + weatherInfor.getString("weather3");
String sports = jsonSports.getString("name") + "(" + jsonSports.getString("index") + ")\n" + jsonSports.getString("details");
String wear = jsonWear.getString("name") + "(" + jsonWear.getString("index") + ")\n" + jsonWear.getString("details");
//保存天气信息到SharedPreferences的文件中
saveWeatherInfor(context, cityName, cityid, date_y, wind1, weather_desp1, weather_desp2, weather_desp3,sports,wear);
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	/**
	 * save data infor into SharedPreferences file
	 * 保存数据信息到SharedPreferences里面,在WeatherActivity的showWeather()方法中好调用
	 */
	public static void saveWeatherInfor(Context context,String cityName,String cityid,String date_y,String wind1,String weather_desp1,String weather_desp2,String weather_desp3,String sports,String wear){
		SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月d日",Locale.CHINA);
		editor.putBoolean("city_selected", true);
		editor.putString("city_name", cityName);
		editor.putString("cityid", cityid);
		editor.putString("date_y", sdf.format(new Date()));
		//String time = sdf.format(new Date());
		editor.putString("wind1", wind1);
		editor.putString("weather_desp1", weather_desp1);
		editor.putString("weather_desp2", weather_desp2);
		editor.putString("weather_desp3", weather_desp3);
		editor.putString("sports", sports);
		editor.putString("wear", wear);
		editor.commit();
	}

执行完后,再返回到queryFromServer方法中run()的showWeather()方法,显示天气信息

private void showWeather() {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		cityNameText.setText(prefs.getString("city_name", ""));
		data_y.setText(prefs.getString("date_y", ""));
		wind1.setText(prefs.getString("wind1", ""));
		weather_desp1.setText(prefs.getString("weather_desp1", ""));
		weather_desp2.setText(prefs.getString("weather_desp2", ""));
		weather_desp3.setText(prefs.getString("weather_desp3", ""));
		sports.setText(prefs.getString("sports", ""));
		wear.setText(prefs.getString("wear", ""));
		weatherInfoLayout.setVisibility(View.VISIBLE);
		cityNameText.setVisibility(View.VISIBLE);
	}

这样就成功的查询到了一个城市的天气信息了。

3 整体调用关系图

最后附上代码下载地址

http://files.cnblogs.com/files/fankongkong/CoolWeather.rar

时间: 2024-10-25 05:05:51

空空天气预报的相关文章

Python天气预报数据获取脚本

近来公司大屏幕的天气接口老是出问题,之前用的是webservice的http://www.webxml.com.cn/WebServices/WeatherWebService.asmx这个接口,坑的是每个月的25号该服务暂停维护,因此不得不寻找新的接口替换... 然后我在网站上找了很多接口(大都是过期的或者非免费的!)...类似 国家气象局提供的天气预报接口http://www.weather.com.cn/data/sk/101010100.html 2014年6月最新更新的http://w

Windows Phone 8.1 开发实例 天气预报

首先感谢林政老师的博客,给了我很大的指导. 准备工作 我的开发环境: - Visual Studio 2013(With Update 4) - Windows Phone 8.1 - Windows 8.1 我使用的是百度天气的api,所以你需要一个百度天气的ak,戳这里申请.记住你ak的值,像这样拼接uri: http://api.map.baidu.com/telematics/v3/weather?location=城市&output=xml&ak=你的ak 可以获取一个xml格式

天气预报API(三):免费接口测试(“旧编码”)

说明 我以参考文章为引子,自己测试并扩展,努力寻找更多的气象API... 本文所有测试均以青岛为例. 本文所列接口城市代码(cityid)参数都使用的 "旧编码":全国城市代码列表(旧) 或 全球城市.景点代码 注:另有 "新编码" 的介绍和使用,详见: 全国城市代码列表(新) 1. 国家气象局 当天基础天气接口 http://www.weather.com.cn/data/sk/101120201.html { "weatherinfo": {

Arduino Tian开发板:一个功能强大的天气预报中心

每天都在出现新的连接设备. Arduino携手云平台一起加入这场战斗,于是出现了一个新的挑战者 - Arduino Tian! 使用python和经典Arduino框架,本教程将引导您将您的Arduino Tian开发板变成一个天气预报中心. 但由于Lucky扩展板有那么多传感器,所以涉及到的内容可能会更多些! 第一部分:所需的设备 ●     Arduino Tian开发板(https://store.arduino.cc/arduino-genuino/arduino-genuino-boa

android应用开发-从设计到实现 3-6 静态原型的天气预报

静态原型的天气预报 未来5天的天气预报,可以看成由5个类似的天气模块构成,只要完成一个,其他几个就可以通过复制+修改内容实现改变. 首先来明确一下各个组件的尺寸和参数, 整个区域的大小,我设定成360dp*100dp: 左右边距设置成16dp: 这样一来,每日的天气模块就是(360-16-16)/ 5 = 65.6dp,我将它们设定成65dp宽: 天气的图标的大小设定成30dp的正方形区域(注意,这些图标不算是系统图标,所以它的尺寸不需要参考设计成24dp的系统图标): 日期的文字,参考Body

微信公众平台开发(21) 天气预报功能开发

原文:http://www.cnblogs.com/imaker/p/5491433.html 一.简介 前面几篇文章对微信公众平台的开通及简单使用做了简单的介绍,但都没有涉及到实际使用中的问题,例如天气查询,公交查询,快递查询等.接下来的几篇文章将对实际生活中会经常使用到的一些功能进行开发讲解,以供读者参考. 这一篇文章将对大家每天都会关心的天气查询进行开发,例如,用户发送消息 "苏州天气",则会返回苏州实时天气状况,以及未来两天甚至未来五天的天气状况. 二.思路分析 首先要对用户发

Android JSON原生解析的几种思路,以号码归属地,笑话大全,天气预报为例演示

Android JSON原生解析的几种思路,以天气预报为例 今天项目中要实现一个天气的预览,加载的信息很多,字段也很多,所以理清了一下思路,准备独立出来写一个总结,这样对大家还是很有帮助的,老司机要开车了 涉及到网络,你就一定要先添加权限,准没错 <!--网络权限--> <uses-permission android:name="android.permission.INTERNET" /> 一.归属地查询(JSONObject) 这个也是最简单的一类Json

delphi 天气预报

天气预报 var astream : tmemorystream; sStream : TStringStream; jv : TJSONValue; begin astream := tmemorystream.Create; sStream := TStringStream.Create( '', tencoding.UTF8 ); self.NetHTTPClient1.Get( 'http://www.weather.com.cn/adat/sk/101010100.html', ast

天气预报接口

网址:http://www.k780.com/api/weather.city天气: 实时天气:http://api.k780.com:88/?app=weather.today&weaid=1&&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json 天气预报(未来5-7天):http://api.k780.com:88/?app=weather.future&weaid=1&&