使用新浪API实现天气状况的显示

1.首先要了解新浪API返回值的含义,http://php.weather.sina.com.cn/xml.php?city=%B1%B1%BE%A9&password=DJOYnieT8234jlsK&day=0


 <?xml version="1.0" encoding="UTF-8"?>
 <!-- published at 2015-10-07 20:21:15 -->
 <Profiles>
 <Weather>
 <city>北京</city>
 <status1>霾</status1>
 <status2>晴</status2>
 <figure1>mai</figure1>
 <figure2>qing</figure2>
 <direction1>无持续风向</direction1>
 <direction2>北风</direction2>
 <power1>≤3</power1>
 <power2>4-5</power2>
 <temperature1>25</temperature1>
 <temperature2>13</temperature2>
 <ssd>6</ssd>
 <tgd1>23</tgd1>
 <tgd2>23</tgd2>
 <zwx>3</zwx>
 <ktk>4</ktk>
 <pollution>1</pollution>
 <xcz>3</xcz>
 <zho></zho>
 <diy></diy>
 <fas></fas>
 <chy>3</chy>
 <zho_shuoming>暂无</zho_shuoming>
 <diy_shuoming>暂无</diy_shuoming>
 <fas_shuoming>暂无</fas_shuoming>
 <chy_shuoming>单层薄衫、裤薄型棉衫、长裤、针织长袖衫、长袖T恤。薄型套装、牛仔衫裤、西服套装、薄型夹克</chy_shuoming>
 <pollution_l>优</pollution_l>
 <zwx_l>中等</zwx_l>
 <ssd_l>凉爽舒适</ssd_l>
 <fas_l>暂无</fas_l>
 <zho_l>暂无</zho_l>
 <chy_l>单衣类</chy_l>
 <ktk_l>不需要开启</ktk_l>
 <xcz_l>比较适宜</xcz_l>
 <diy_l>暂无</diy_l>
 <pollution_s>非常有利于空气污染物扩散</pollution_s>
 <zwx_s>紫外线中等</zwx_s>
 <ssd_s>天气状况良好时,多到户外活动,并可适当增加户外活动时间。</ssd_s>
 <ktk_s>不需要开启空调</ktk_s>
 <xcz_s>洗车后未来2天内没有降水、大风或沙尘天气,比较适宜洗车</xcz_s>
 <gm>1</gm>
 <gm_l>低发期</gm_l>
 <gm_s>天气舒适,不易发生感冒;</gm_s>
 <yd>1</yd>
 <yd_l>非常适宜</yd_l>
 <yd_s>人们感觉舒适,非常适宜户外运动;</yd_s>
 <savedate_weather>2015-10-07</savedate_weather>
 <savedate_life>2015-10-07</savedate_life>
 <savedate_zhishu>2015-10-07</savedate_zhishu>
 <udatetime>2015-10-07 17:10:49</udatetime>
 </Weather>
 </Profiles>
 


标签中1表示白天,2表示夜间

 

<status>

< figure>

<direction>

<power>

<temperature>

<ssd>

<ssd_l>

<ssd_s>

<tgd>

<zwx>

<zwx_l>

<zwx_s>

<ktk>

<ktk_l>

<ktk_s>

<pollution>

<pollution_l>

<pollution_s>

<xcz>

<xcz_l>

<xcz_s>

<chy>

<chy_l>

<chy_shuoming>

<gm>

<gm_l>

<gm_s>

<yd>

<yd_l>

<yd_s>

<zho>

<zho_l>

<zho_shuoming>

<diy>

<diy_l>

<diy_shuoming>

<fas>

<fas_l>

<fas_shuoming>

<savedate_weather>

<savedate_life>

<savedate_zhishu>


天气情况中文

天气情况拼音

风向

风级

温度

体感指数数值

体感度指数

体感度指数说明

体感温度

紫外线指数数值

紫外线指数

紫外线指数说明

空调指数数值

空调指数

 空调指数说明

污染指数数值

污染物扩散指数

污染指数说明

洗车指数数值

洗车指数

洗车指数说明

洗车指数数值

穿衣指数

穿衣说明

感冒指数数值

感冒指数

感冒指数说明

运动指数数值

运动指数

运动指数说明

 

 

 

 

 

 

 

 

 

天气预报日期

生活日期

指数日期

 2.由于返回的是XML所以要做XML解析

先声明天气类:

package com.example.tool;

public class Weather {
	private String city;
	private int temperature2;
	private int temperature1;
	private String status1;
	private String status2;
	private String direction1;
	private String direction2;
	private String power1;
	private String power2;
	private String xcz_s;
	private String yd_s;
	private String gm;

	private String tgd;
	private String pollution;
	private String zwx;
	private String chy_shuoming;
	private String gm_l;
	private String gm_s;
private String ssd_s;

	public String getSsd_s() {
	return ssd_s;
}

public void setSsd_s(String ssd_s) {
	this.ssd_s = ssd_s;
}

	public String getGm_l() {
		return gm_l;
	}

	public void setGm_l(String gm_l) {
		this.gm_l = gm_l;
	}

	public String getGm_s() {
		return gm_s;
	}

	public void setGm_s(String gm_s) {
		this.gm_s = gm_s;
	}

	public String getChy_shuoming() {
		return chy_shuoming;
	}

	public void setChy_shuoming(String chy_shuoming) {
		this.chy_shuoming = chy_shuoming;
	}

	public String getZwx() {
		return zwx;
	}

	public void setZwx(String zwx) {
		this.zwx = zwx;
	}

	public String getPollution() {
		return pollution;
	}

	public void setPollution(String pollution) {
		this.pollution = pollution;
	}

	public String getTgd() {
		return tgd;
	}

	public void setTgd(String tgd) {
		this.tgd = tgd;
	}

	public String getGm() {
		return gm;
	}

	public void setGm(String gm) {
		this.gm = gm;
	}

	public String getYd_s() {
		return yd_s;
	}

	public void setYd_s(String yd_s) {
		this.yd_s = yd_s;
	}

	public String getXcz_s() {
		return xcz_s;
	}

	public void setXcz_s(String xcz_s) {
		this.xcz_s = xcz_s;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public int getTemperature2() {
		return temperature2;
	}

	public void setTemperature2(int temperature2) {
		this.temperature2 = temperature2;
	}

	public int getTemperature1() {
		return temperature1;
	}

	public void setTemperature1(int temperature1) {
		this.temperature1 = temperature1;
	}

	public String getStatus1() {
		return status1;
	}

	public void setStatus1(String status1) {
		this.status1 = status1;
	}

	public String getStatus2() {
		return status2;
	}

	public void setStatus2(String status2) {
		this.status2 = status2;
	}

	public String getDirection1() {
		return direction1;
	}

	public void setDirection1(String direction1) {
		this.direction1 = direction1;
	}

	public String getDirection2() {
		return direction2;
	}

	public void setDirection2(String direction2) {
		this.direction2 = direction2;
	}

	public String getPower1() {
		return power1;
	}

	public void setPower1(String power1) {
		this.power1 = power1;
	}

	public String getPower2() {
		return power2;
	}

	public void setPower2(String power2) {
		this.power2 = power2;
	}
}

解析XML:

package com.example.tool;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.content.Context;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

//读取XML文件信息
public class Configration {
	public Weather readInfo(InputStream a) {
		try {
			SAXParserFactory factory = SAXParserFactory.newInstance(); // 取得SAXParserFactory实例
			SAXParser parser = factory.newSAXParser();
			MyHandler handler = new MyHandler();
			parser.parse(a, handler);
			a.close();
			return handler.getWeather();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public String EcodingGB2312(String a) throws UnsupportedEncodingException {
		return URLEncoder.encode(a, "gb2312");
	}

	private class MyHandler extends DefaultHandler {
		private Weather weather = null;
		private String tagName = null;// 当前解析的元素标签

		public Weather getWeather() {
			return weather;
		}

		@Override
		public void startDocument() throws SAXException {
			System.out.println("ERROR");
			weather = new Weather();
		}

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			this.tagName = localName;

		}

		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			if (this.tagName != null) {
				String data = new String(ch, start, length);
				if (this.tagName.equals("city")) {
					weather.setCity(data);
				} else if (this.tagName.equals("temperature2")) {
					weather.setTemperature2(Integer.valueOf(data));
				} else if (this.tagName.equals("temperature1")) {
					weather.setTemperature1(Integer.valueOf(data));
				} else if (this.tagName.equals("status1")) {
					weather.setStatus1(data);
				} else if (this.tagName.equals("status2")) {
					weather.setStatus2(data);
				} else if (this.tagName.equals("direction1")) {
					weather.setDirection1(data);
				} else if (this.tagName.equals("direction2")) {
					weather.setDirection2(data);
				} else if (this.tagName.equals("power1")) {
					weather.setPower1(data);
				} else if (this.tagName.equals("power2")) {
					weather.setPower2(data);
				} else if (this.tagName.equals("xcz_s")) {
					weather.setXcz_s(data);
				} else if (this.tagName.equals("yd_s")) {
					weather.setYd_s(data);
				} else if (this.tagName.equals("gm")) {
					weather.setGm(data);
					// 体感
				} else if (this.tagName.equals("tgd1")) {
					weather.setTgd(data);
					// 污染指数
				} else if (this.tagName.equals("pollution")) {
					weather.setPollution(data);
					// 紫外线
				} else if (this.tagName.equals("zwx")) {
					weather.setZwx(data);
				} else if (this.tagName.equals("chy_shuoming")) {
					weather.setChy_shuoming(data);
				} else if (this.tagName.equals("gm_l")) {
					weather.setGm_l(data);
				} else if (this.tagName.equals("gm_s")) {
					weather.setGm_s(data);
				} else if (this.tagName.equals("ssd_s")) {
					weather.setSsd_s(data);
				}
			}

		}

		@Override
		public void endElement(String uri, String localName, String name)
				throws SAXException {
			this.tagName = null;
		}
	}


}
3.通过API得到一系列指数

package com.example.tool;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;

import android.util.Log;

public class GetWeather {
	/**
	 * 获取天气tips
	 * 
	 * @return 天气和tips字符串
	 */
	public String getWeather(String city) {
		String url = "http://php.weather.sina.com.cn/xml.php?city=" + city
				+ "&password=DJOYnieT8234jlsK&day=0";
		HttpResponse httpResponse = null;
		StringBuffer result = null;
		DefaultHttpClient client = null;
		try {
			client = new DefaultHttpClient();
			client.getParams().setParameter(
					CoreConnectionPNames.CONNECTION_TIMEOUT, 15000);
			client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
					15000);
			HttpGet httpGet = new HttpGet(url);
			httpResponse = client.execute(httpGet);
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				result = new StringBuffer("");
				String s = EntityUtils.toString(httpResponse.getEntity(),
						"UTF-8");
				InputStream in_withcode = new ByteArrayInputStream(
						s.getBytes("UTF-8"));
				Weather w = new Configration().readInfo(in_withcode);
				if (w != null) {
					result.append(w.getCity());// 城市
					result.append(" 天气状况:" + w.getStatus1());// 天气状况
					result.append("转" + w.getStatus2() + " ");// 天气状况
					result.append(w.getDirection1() + w.getPower1() + "级~");// 风
					result.append(w.getDirection2() + w.getPower2() + "级 ");// 风
					result.append("温度:" + w.getTemperature2() + "~"
							+ w.getTemperature1() + "℃");// 低温
					result.append(" 户外运动:" + w.getYd_s());
					result.append("体感" + w.getTgd());
					result.append("污染" + w.getPollution());
					result.append("紫外线" + w.getZwx());
					result.append("穿衣指南:" + w.getChy_shuoming());
					result.append("温馨提示:感冒" + w.getGm_l()+","+w.getGm_s()+w.getSsd_s());
				}
			} else {
				Log.i("note", "数据请求异常!");
				result = null;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (result != null)
			return result.toString();
		else
			return null;
	}

}

<span style="color:#ff0000;">String city:代表要查询的城市</span>
<span style="color:#ff0000;">由于API需要GB2312编码的城市名</span>
<span style="color:#ff0000;">所以通过</span>
<span style="color:#ff0000;">public String EcodingGB2312(String a) throws UnsupportedEncodingException {
<span style="white-space:pre">		</span>return URLEncoder.encode(a, "gb2312");
<span style="white-space:pre">	</span>}
</span>
<span style="color:#ff0000;">将城市输入返回一个GB2312编码的城市然后调用</span><pre name="code" class="java" style="font-size: 14px; line-height: 26px; "><span style="color:#ff0000;"></span><pre name="code" class="java" style="font-size: 14px; line-height: 26px; "> getWeather(String city) 

 
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值