天气信息

1.国家气象局开放接口:

点击打开链接

下面是自己集成的功能,根据城市的名称获取该城市的今日天气。

/**
 * 天气测试
 */
public class TestWeather extends Activity implements OnClickListener {
	private EditText editText;
	private Button sureBtn;
	private TextView infoText;

	private CityWeatherInfo cityWeather;
	private Handler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acy_testweather);

		editText = (EditText) findViewById(R.id.ed_city);
		sureBtn = (Button) findViewById(R.id.btn_sure);
		infoText = (TextView) findViewById(R.id.cityinfo);

		sureBtn.setOnClickListener(this);

		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				if (0 == msg.what) {
					Bundle bundle = msg.getData();
					infoText.setText(bundle.getString("info"));
				}
				super.handleMessage(msg);
			}
		};
	}

	/**
	 * 根据城市的名字 获取城市的id号码
	 */
	public int getCityID(String cityname) {
		try {
			InputStream inputStream = getResources().getAssets().open(
					"xml_cityid.xml");
			// 获取实例
			XmlPullParser xmlPullParser = XmlPullParserFactory.newInstance()
					.newPullParser();
			// 设置解析的xml
			xmlPullParser.setInput(inputStream, "UTF-8");
			// 取得事件
			int event = xmlPullParser.getEventType();
			// 开始循环解析xml元素
			while (event != xmlPullParser.END_DOCUMENT) {
				String Node = xmlPullParser.getName();
				switch (event) {
				case XmlPullParser.START_TAG:
					// 当节点时 "country" 的时候 才进行操作
					if ("county".equals(Node)) {
						// 当你查询城市名 跟当前节点的城市名相同的时候
						if (cityname.equals(xmlPullParser.getAttributeValue(1))) {
							return Integer.parseInt(xmlPullParser
									.getAttributeValue(2));
						}
					}
					break;
				default:
					break;
				}
				event = xmlPullParser.next();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return 0;
	}

	/**
	 * 根据城市的id 获取城市的天气信息
	 */
	public String getCityInfo(int cityid) {
		if (cityid == 0) {
			Toast.makeText(this, "抱歉 ,请输入正确的城市名称", 1000).show();
			return "";
		}
		// 获取gson实例
		Gson gson = new Gson();
		URL url = null;
		InputStreamReader reader = null;
		BufferedReader bufferedReader = null;
		StringBuffer buffer = null;
		try {
			url = new URL("http://www.weather.com.cn/data/cityinfo/" + cityid
					+ ".html");
			URLConnection urlconn = url.openConnection();
			reader = new InputStreamReader(urlconn.getInputStream());
			bufferedReader = new BufferedReader(reader);
			buffer = new StringBuffer();
			String line = null;
			while ((line = bufferedReader.readLine()) != null) {
				buffer.append(line);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				reader.close();
				bufferedReader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		// 将json字符串转换为对象
		Type getType = new TypeToken<Map<String, CityWeatherInfo>>() {
		}.getType();
		Map<String, CityWeatherInfo> mycity = gson.fromJson(buffer.toString(),
				getType);
		for (Map.Entry<String, CityWeatherInfo> entry : mycity.entrySet()) {
			cityWeather = entry.getValue();
		}
		// 将天气信息返回
		return cityWeather.toString();
	}

	class MyThread implements Runnable {
		private int cityId;

		public MyThread(int id) {
			this.cityId = id;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			String info = getCityInfo(cityId);
			Bundle bundle = new Bundle();
			bundle.putString("info", info);
			Message msg = new Message();
			msg.what = 0;
			msg.setData(bundle);
			handler.sendMessage(msg);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == sureBtn) {
			MyThread myThread = new MyThread(getCityID(editText.getText()
					.toString()));
			Thread thread = new Thread(myThread);
			thread.start();
		}
	}
}

效果:

        1.测试成都的天气状况:

        2.测试北京的天气状况:



注意:

        1.我这里转换json字符为对象,用的是Gson,记得哦,不然的话可能效果达不到你的期望。

        2.xml存在于assets目录下。

        3.xml文件,我分享在了资源里面。


2.google api

点击打开链接

3 java

点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值