可传入多个城市,用空格(%2C)分开
http://php.weather.sina.com.cn/iframe/index/w_cl.php?code=js&day=0&city=%E5%8C%97%E4%BA%AC%2C%E4%B8%8A%E6%B5%B7&dfc=1&random=1434619840774
http://php.weather.sina.com.cn/iframe/index/w_cl.php?code=js&day=0&city=北京%2C上海&dfc=1&random=1434619840774
返回:
(function() {
var w = [];
w['北京'] = [{
s1: '多云',
s2: '雷阵雨',
f1: 'duoyun',
f2: 'leizhenyu',
t1: '31',
t2: '19',
p1: '3-4',
p2: '≤3',
d1: '北风',
d2: '无持续风向'
}];
w['上海'] = [{
s1: '小雨',
s2: '多云',
f1: 'xiaoyu',
f2: 'duoyun',
t1: '25',
t2: '20',
p1: '≤3',
p2: '≤3',
d1: '东北风',
d2: '东北风'
}];
var add = {
now: '2015-06-18 17:36:15',
time: '1434620175',
update: '北京时间06月18日08:20更新',
error: '0',
total: '2'
};
window.SWther = {
w: w,
add: add
};
})(); //0
public static String cityweather(String city) {
String url="";
String vhtml="";
String weather="";
try {
vhtml="这个是返回的html";
System.out.println(vhtml);
String city_s[]=city.split("\\+");
String update=getSptderContent(vhtml, "1update:'([^<>,]+)',");
for (int i = 0; i < city_s.length; i++) {
Pattern p=Pattern.compile("("+city_s[i]+"'.*?;)");
Matcher m=p.matcher(vhtml);
if (m.find()) {
String dd=m.group(1);
String day_s1=getSptderContent(dd, "1s1:'([^<>,]+)',");//白天
String day_s2=getSptderContent(dd, "1s2:'([^<>,]+)',");//晚上
String day_t1=getSptderContent(dd, "1t1:'([^<>,]+)',");//白天温度
String day_t2=getSptderContent(dd, "1t2:'([^<>,]+)',");//晚上温度
String day_d1=getSptderContent(dd, "1d1:'([^<>,]+)',");//白天
String day_d2=getSptderContent(dd, "1d2:'([^<>,]+)',");//晚上
String day_p1=getSptderContent(dd, "1p1:'([^<>,]+)',");//白天风速
String day_p2=getSptderContent(dd, "1p2:'([^<>,]+)',");//晚上
weather+="【"+city_s[i]+"】白天:"+day_s1+day_t1+"℃ "+day_d1+day_p1+"级,夜间:"+day_s2+day_t2+"℃ "+day_d2+day_p2+"级;";
}
}
//System.out.println(weather);
return weather+update;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return weather;
}
public static String getSptderContent(String text,String reg){
String content="";
int cou=Integer.parseInt(reg.substring(0,1));
String regex=reg.substring(1,reg.length());
Pattern p2 = Pattern.compile(regex);
Matcher m2 = p2.matcher(text);
if(m2.find()){
content=m2.group(cou);
}
String regEx_html = "<[^>]+>";
if (content!=null) {
content=content.replaceAll(regEx_html, "");
}
return content;
}