说明:
1. 城市名写死了,取的shenzhen,这个需要自己取;
2. 使用到Jsoup开源解析框架,这个自己去下载;
3. 第一步先获取城市的Code
public void initYahooWeatherData(){
new Thread(new Runnable() {
@Override
public void run() {
Document getCode;
String getCityCode = "http://sugg.us.search.yahoo.net/gossip-gl-location/?appid=weather&output=xml&command="+"shenzhen";
try {
getCode = Jsoup.connect(getCityCode).get();
Elements image = getCode.select("s");
String mCityCode = null;
for(Element el : image){
String desiresStr = el.attr("d").toString();
if(desiresStr.contains("woeid=")){
desiresStr = desiresStr.substring(desiresStr.indexOf("woeid="),desiresStr.length());
mCityCode = desiresStr.substring(desiresStr.indexOf("=")+1,desiresStr.indexOf("&"));
break;
}
}
Log.i("获取天气-城市Code", mCityCode+"---");
if(!TextUtils.isEmpty(mCityCode)){
Document mWeatherCode;
String getWeatherCode = "http://weather.yahooapis.com/forecastrss?w="+mCityCode+"&u=c";
try {
mWeatherCode = Jsoup.connect(getWeatherCode).get();
Log.i("获取天气", mWeatherCode+"---");
} catch (IOException e) {
// TODO: handle exception
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();;
}