public class OpenUrl { public static void main(String[] args){ OpenUrl openUrl = new OpenUrl(); Weather w = openUrl.getWeatherByUrl("http://weather.tq121.com.cn/detail.php?city=武汉"); System.out.println(w.getDate1()); System.out.println(w.getTemperature1()); System.out.println(w.getWeather1()); System.out.println(w.getWind1());
public class OpenUrl { public static void main(String[] args){ OpenUrl openUrl = new OpenUrl(); Weather w = openUrl.getWeatherByUrl("http://weather.tq121.com.cn/detail.php?city=武汉"); System.out.println(w.getDate1()); System.out.println(w.getTemperature1()); System.out.println(w.getWeather1()); System.out.println(w.getWind1());
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.zdao.weather.bean.Weather;
public class OpenUrl {
public static void main(String[] args){
OpenUrl openUrl = new OpenUrl();
Weather w = openUrl.getWeatherByUrl("http://weather.tq121.com.cn/detail.php?city=武汉");
System.out.println(w.getDate1());
System.out.println(w.getTemperature1());
System.out.println(w.getWeather1());
System.out.println(w.getWind1());
System.out.println(w.getDate2());
System.out.println(w.getTemperature2());
System.out.println(w.getWeather2());
System.out.println(w.getWind2());
System.out.println(w.getDate3());
System.out.println(w.getTemperature3());
System.out.println(w.getWeather3());
System.out.println(w.getWind3());
System.out.println(w.getDate4());
System.out.println(w.getTemperature4());
System.out.println(w.getWeather4());
System.out.println(w.getWind4());
System.out.println(w.getDate5());
System.out.println(w.getTemperature5());
System.out.println(w.getWeather5());
System.out.println(w.getWind5());
}
/**
* 根据连接抓出5天天气
*/
public Weather getWeatherByUrl(String url){
Weather weather = new Weather();
try {
long l1 = System.currentTimeMillis();
String content = getContent(url);
weather = getDateTemperature(content , weather);
weather = getWind3(content , weather);
weather = getWeather3(content , weather);
weather = getDate(content , weather);
weather = getTemperature(content , weather);
weather = getWind2(content , weather);
weather = getWeather2(content , weather);
long l2 = System.currentTimeMillis();
System.out.println(l2 - l1);
}catch (Exception e){
System.out.println(e);
}
return weather;
}
/**
* 根据连接地址抓出页面内容
* @param 根据一个连接地址
* @return 页面内容
*/
private String getContent(String strUrl){
try{
URL url = new URL(strUrl);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String s = "";
StringBuffer sb=new StringBuffer();
while((s = br.readLine())!=null){
sb.append(s+"\r\n");
}
br.close();
return sb.toString();
}catch(Exception e){
return "error open url" + strUrl;
}
}
/**
* 取当前3天内的温度和时间
* 返回一个天气bean
* 0和1 2个参数为第一天时间和温度 后面相同
* content 为页面内容
* weather 为天气bean
*/
private Weather getDateTemperature(String content , Weather weather) throws Exception{
String[] s = analysis("<td width=\"215\" align=\"center\" valign=\"middle\"><span class=\"big-cn\">(.*?)</span></td>", content , 6);
weather.setDate1(s[0].trim());
weather.setTemperature1(s[1].trim());
weather.setDate2(s[2].trim());
weather.setTemperature2(s[3].trim());
weather.setDate3(s[4].trim());
weather.setTemperature3(s[5].trim());
return weather;
}
/**
* 取前3天的风向
* content 为页面内容
* weather 为天气bean
*/
private Weather getWind3(String content , Weather weather){
String[] s = analysis("<td width=\"215\" align=\"center\" valign=\"middle\"><span class=\"cn\">(.*?)</span></td>", content , 3);
weather.setWind1(s[0].trim());
weather.setWind2(s[1].trim());
weather.setWind3(s[2].trim());
return weather;
}
/**
* 取前3天的天气情况
* content 为页面内容
* weather 为天气bean
*/
private Weather getWeather3(String content , Weather weather){
String[] s = analysis("align=\"center\" valign=\"top\"><img src=\"../images/a(.*?).gif\" width=\"70\" height=\"65\"></td>" , content , 6);
s = ConversionWeather(s);
weather.setWeather1(s[0]);
weather.setWeather2(s[1]);
weather.setWeather3(s[2]);
return weather;
}
/**
* 取后2天的日期
*/
private Weather getDate(String content , Weather weather){
String[] s = analysis("<td width=\"121\"><span class=\"cn\">(.*?)</span></td>" , content , 2);
weather.setDate4(s[0].trim());
weather.setDate5(s[1].trim());
return weather;
}
/**
* 取后天2天的温度
*/
private Weather getTemperature(String content , Weather weather){
String[] s = analysis("<td width=\"86\" class=\"cn\"><span class=\"wendu\">(.*?)</span></td>" , content , 2);
weather.setTemperature4(s[0].trim());
weather.setTemperature5(s[1].trim());
return weather;
}
/**
* 取后天2天的风向
*/
private Weather getWind2(String content , Weather weather){
String[] s = analysis("<td width=\"157\"><span class=\"cn\">(.*?)</span></td>" , content , 2);
weather.setWind4(s[0].trim());
weather.setWind5(s[1].trim());
return weather;
}
/**
*
*/
private Weather getWeather2(String content , Weather weather){
String[] s = analysis("<img src=\"../images/b(.*?).gif\" width=\"50\" height=\"46\"></td>" , content , 4);
s = ConversionWeather(s);
weather.setWeather4(s[0]);
weather.setWeather5(s[1]);
return weather;
}
/**
* 根据页面内容和正则表达式来分析页面,得到分析结果
* @param pattern 正则表达式
* @param match 页面内容
* @return content 结果
*/
private String[] analysis(String pattern, String match , int i){
Pattern sp = Pattern.compile(pattern);
Matcher matcher = sp.matcher(match);
String[] content = new String[i];
for (int i1 = 0; matcher.find(); i1++){
content[i1] = matcher.group(1);
}
return content;
}
/**
* 转换天气情况
* @param s第1张图片
* @return 天气情况
*/
private String[] ConversionWeather(String s[]){
String[] s1 = {"晴天", "多云", "阴", "阵雨", "雷阵雨", "雷阵雨并伴有冰雹", "雨加雪", "小雨", "中雨", "大雨", "暴雨", "大暴雨", "特大暴雨", "阵雪", "小雪", "中雪", "大雪", "暴雪", "雾", "冻雨", "沙尘暴", "小雨-中雨", "中雨-大雨", "大雨-暴雨", "暴雨-大暴雨", "大暴雨-特大暴雨", "小雪-中雪", "中雪-大雪", "大雪-暴雪", "浮尘", "扬沙", "强沙尘暴"};
String[] s2 = new String[s.length/2];
int i1 = 0;
for (int i = 0; i < s.length; i += 2){
if (s[i].trim().equals(s[i+1].trim())){
s2[i1] = s1[Integer.parseInt(s[i])];
}else {
s2[i1] = s1[Integer.parseInt(s[i])] + "转" + s1[Integer.parseInt(s[i+1])];
}
i1++;
}
return s2;
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.zdao.weather.bean.Weather;
public class OpenUrl {
public static void main(String[] args){
OpenUrl openUrl = new OpenUrl();
Weather w = openUrl.getWeatherByUrl("http://weather.tq121.com.cn/detail.php?city=武汉");
System.out.println(w.getDate1());
System.out.println(w.getTemperature1());
System.out.println(w.getWeather1());
System.out.println(w.getWind1());
System.out.println(w.getDate2());
System.out.println(w.getTemperature2());
System.out.println(w.getWeather2());
System.out.println(w.getWind2());
System.out.println(w.getDate3());
System.out.println(w.getTemperature3());
System.out.println(w.getWeather3());
System.out.println(w.getWind3());
System.out.println(w.getDate4());
System.out.println(w.getTemperature4());
System.out.println(w.getWeather4());
System.out.println(w.getWind4());
System.out.println(w.getDate5());
System.out.println(w.getTemperature5());
System.out.println(w.getWeather5());
System.out.println(w.getWind5());
}
/**
* 根据连接抓出5天天气
*/
public Weather getWeatherByUrl(String url){
Weather weather = new Weather();
try {
long l1 = System.currentTimeMillis();
String content = getContent(url);
weather = getDateTemperature(content , weather);
weather = getWind3(content , weather);
weather = getWeather3(content , weather);
weather = getDate(content , weather);
weather = getTemperature(content , weather);
weather = getWind2(content , weather);
weather = getWeather2(content , weather);
long l2 = System.currentTimeMillis();
System.out.println(l2 - l1);
}catch (Exception e){
System.out.println(e);
}
return weather;
}
/**
* 根据连接地址抓出页面内容
* @param 根据一个连接地址
* @return 页面内容
*/
private String getContent(String strUrl){
try{
URL url = new URL(strUrl);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String s = "";
StringBuffer sb=new StringBuffer();
while((s = br.readLine())!=null){
sb.append(s+"\r\n");
}
br.close();
return sb.toString();
}catch(Exception e){
return "error open url" + strUrl;
}
}
/**
* 取当前3天内的温度和时间
* 返回一个天气bean
* 0和1 2个参数为第一天时间和温度 后面相同
* content 为页面内容
* weather 为天气bean
*/
private Weather getDateTemperature(String content , Weather weather) throws Exception{
String[] s = analysis("<td width=\"215\" align=\"center\" valign=\"middle\"><span class=\"big-cn\">(.*?)</span></td>", content , 6);
weather.setDate1(s[0].trim());
weather.setTemperature1(s[1].trim());
weather.setDate2(s[2].trim());
weather.setTemperature2(s[3].trim());
weather.setDate3(s[4].trim());
weather.setTemperature3(s[5].trim());
return weather;
}
/**
* 取前3天的风向
* content 为页面内容
* weather 为天气bean
*/
private Weather getWind3(String content , Weather weather){
String[] s = analysis("<td width=\"215\" align=\"center\" valign=\"middle\"><span class=\"cn\">(.*?)</span></td>", content , 3);
weather.setWind1(s[0].trim());
weather.setWind2(s[1].trim());
weather.setWind3(s[2].trim());
return weather;
}
/**
* 取前3天的天气情况
* content 为页面内容
* weather 为天气bean
*/
private Weather getWeather3(String content , Weather weather){
String[] s = analysis("align=\"center\" valign=\"top\"><img src=\"../images/a(.*?).gif\" width=\"70\" height=\"65\"></td>" , content , 6);
s = ConversionWeather(s);
weather.setWeather1(s[0]);
weather.setWeather2(s[1]);
weather.setWeather3(s[2]);
return weather;
}
/**
* 取后2天的日期
*/
private Weather getDate(String content , Weather weather){
String[] s = analysis("<td width=\"121\"><span class=\"cn\">(.*?)</span></td>" , content , 2);
weather.setDate4(s[0].trim());
weather.setDate5(s[1].trim());
return weather;
}
/**
* 取后天2天的温度
*/
private Weather getTemperature(String content , Weather weather){
String[] s = analysis("<td width=\"86\" class=\"cn\"><span class=\"wendu\">(.*?)</span></td>" , content , 2);
weather.setTemperature4(s[0].trim());
weather.setTemperature5(s[1].trim());
return weather;
}
/**
* 取后天2天的风向
*/
private Weather getWind2(String content , Weather weather){
String[] s = analysis("<td width=\"157\"><span class=\"cn\">(.*?)</span></td>" , content , 2);
weather.setWind4(s[0].trim());
weather.setWind5(s[1].trim());
return weather;
}
/**
*
*/
private Weather getWeather2(String content , Weather weather){
String[] s = analysis("<img src=\"../images/b(.*?).gif\" width=\"50\" height=\"46\"></td>" , content , 4);
s = ConversionWeather(s);
weather.setWeather4(s[0]);
weather.setWeather5(s[1]);
return weather;
}
/**
* 根据页面内容和正则表达式来分析页面,得到分析结果
* @param pattern 正则表达式
* @param match 页面内容
* @return content 结果
*/
private String[] analysis(String pattern, String match , int i){
Pattern sp = Pattern.compile(pattern);
Matcher matcher = sp.matcher(match);
String[] content = new String[i];
for (int i1 = 0; matcher.find(); i1++){
content[i1] = matcher.group(1);
}
return content;
}
/**
* 转换天气情况
* @param s第1张图片
* @return 天气情况
*/
private String[] ConversionWeather(String s[]){
String[] s1 = {"晴天", "多云", "阴", "阵雨", "雷阵雨", "雷阵雨并伴有冰雹", "雨加雪", "小雨", "中雨", "大雨", "暴雨", "大暴雨", "特大暴雨", "阵雪", "小雪", "中雪", "大雪", "暴雪", "雾", "冻雨", "沙尘暴", "小雨-中雨", "中雨-大雨", "大雨-暴雨", "暴雨-大暴雨", "大暴雨-特大暴雨", "小雪-中雪", "中雪-大雪", "大雪-暴雪", "浮尘", "扬沙", "强沙尘暴"};
String[] s2 = new String[s.length/2];
int i1 = 0;
for (int i = 0; i < s.length; i += 2){
if (s[i].trim().equals(s[i+1].trim())){
s2[i1] = s1[Integer.parseInt(s[i])];
}else {
s2[i1] = s1[Integer.parseInt(s[i])] + "转" + s1[Integer.parseInt(s[i+1])];
}
i1++;
}
return s2;
}
}
我想要的是通过webservice的接口来获取信息,不知代码如何写
http://www.webservicex.net/globalweather.asmx?WSDL
try
{
String endpoint ="http://www.wopos.com/webservice/Weather.asmx?wsdl";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress( new java.net.URL(endpoint) );
call.setOperationName(new QName("http://tempuri.org/", "getWeather")); call.setUseSOAPAction(true);
call.setSOAPActionURI("http://tempuri.org/getWeather");
String ret = (String) call.invoke( new Object[] { "武汉" } );
System.out.println("Sent ’Hello!’, got ’" + ret + "’");
} catch (Exception e) {
System.out.println(e.toString());
}
结果报错了:
System.Web.Services.Protocols.SoapException: 服务器无法处理请求。 ---> System.NullReferenceException: 未将对象引用设置到对象的实例。
在 Weather.getWeather(String mCity)
--- 内部异常堆栈跟踪的结尾 ---不知哪里错了,是否漏掉了什么...
<Table>
<Country>China</Country>
<City>Beijing</City>
</Table>
<Table>
<Country>China</Country>
<City>Hohhot</City>
</Table>
<Table>
<Country>China</Country>
<City>Tianjin / Zhangguizhu</City>
</Table>
<Table>
<Country>China</Country>
<City>Taiyuan</City>
</Table>
<Table>
<Country>China</Country>
<City>Changsha</City>
</Table>
<Table>
<Country>China</Country>
<City>Guangzhou</City>
</Table>
<Table>
<Country>China</Country>
<City>Haikou</City>
</Table>
<Table>
<Country>China</Country>
<City>Guilin</City>
</Table>
<Table>
<Country>China</Country>
<City>Nanning</City>
</Table>
<Table>
<Country>China</Country>
<City>Shantou</City>
</Table>
<Table>
<Country>China</Country>
<City>Shenzhen</City>
</Table>
<Table>
<Country>China</Country>
<City>Zhanjiang</City>
</Table>
<Table>
<Country>China</Country>
<City>Zhengzhou</City>
</Table>
<Table>
<Country>China</Country>
<City>Wuhan</City>
</Table>
<Table>
<Country>China</Country>
<City>Yinchuan</City>
</Table>
<Table>
<Country>China</Country>
<City>Jiuquan</City>
</Table>
<Table>
<Country>China</Country>
<City>Xi'An</City>
</Table>
<Table>
<Country>China</Country>
<City>Xining</City>
</Table>
<Table>
<Country>China</Country>
<City>Yan An</City>
</Table>
<Table>
<Country>China</Country>
<City>Kunming</City>
</Table>
<Table>
<Country>China</Country>
<City>Xiamen</City>
</Table>
<Table>
<Country>China</Country>
<City>Nanchang</City>
</Table>
<Table>
<Country>China</Country>
<City>Fuzhou</City>
</Table>
<Table>
<Country>China</Country>
<City>Ganzhou</City>
</Table>
<Table>
<Country>China</Country>
<City>Hangzhou</City>
</Table>
<Table>
<Country>China</Country>
<City>Nanjing</City>
</Table>
<Table>
<Country>China</Country>
<City>Hefei</City>
</Table>
<Table>
<Country>China</Country>
<City>Qingdao</City>
</Table>
<Table>
<Country>China</Country>
<City>Shanghai / Hongqiao</City>
</Table>
<Table>
<Country>China</Country>
<City>Jinan</City>
</Table>
<Table>
<Country>China</Country>
<City>Chongqing</City>
</Table>
<Table>
<Country>China</Country>
<City>Guiyang</City>
</Table>
<Table>
<Country>China</Country>
<City>Lhasa</City>
</Table>
<Table>
<Country>China</Country>
<City>Chengdu</City>
</Table>
<Table>
<Country>China</Country>
<City>Hami</City>
</Table>
<Table>
<Country>China</Country>
<City>Kashi</City>
</Table>
<Table>
<Country>China</Country>
<City>Hotan</City>
</Table>
<Table>
<Country>China</Country>
<City>Urum-Qi / Diwopu</City>
</Table>
<Table>
<Country>China</Country>
<City>Yining</City>
</Table>
<Table>
<Country>China</Country>
<City>Changchun</City>
</Table>
<Table>
<Country>China</Country>
<City>Qiqihar</City>
</Table>
<Table>
<Country>China</Country>
<City>Dalian</City>
</Table>
</NewDataSet>
可以使用netBeans 生成客户端存根
然后在程序中调用存根比较方便
我用myeclipse
new - Web Service -Web Service Client 选择自己的项目
Service Definition 选择
WSDL URL:http://www.webservicex.net/globalweather.asmx?WSDL 选择Java Source 和Java packageFinish之后自动在Java package生成两个文件GlobalWeatherClient和GlobalWeatherSoap
一个包net.webservicex里面有
GetCitiesByCountry
GetCitiesByCountryResponse
GetWeather
GetWeatherResponse
ObjectFactory
package-info
几个类
打开GlobalWeatherClient类在 main 里添加 System.out.println(service.getCitiesByCountry("China"));
运行就可以从webservice获得China的所有城市,其他的获得某个城市天气预报函数也都通过GlobalWeatherSoap中相应函数调用就可以了.返回的数据就是xml格式的,剩下的工作就是通过DOM解析xml显示到页面上了,跟webservice没有关系了<NewDataSet>
<Table>
<Country>China</Country>
<City>Beijing</City>
</Table>
<Table>
<Country>China</Country>
<City>Hohhot</City>
</Table>
<Table>
<Country>China</Country>
<City>Tianjin / Zhangguizhu</City>
</Table>
<Table>
<Country>China</Country>
<City>Taiyuan</City>
</Table>
<Table>
<Country>China</Country>
<City>Changsha</City>
</Table>
<Table>
<Country>China</Country>
<City>Guangzhou</City>
</Table>
<Table>
<Country>China</Country>
<City>Haikou</City>
</Table>
<Table>
<Country>China</Country>
<City>Guilin</City>
</Table>
<Table>
<Country>China</Country>
<City>Nanning</City>
</Table>
<Table>
<Country>China</Country>
<City>Shantou</City>
</Table>
<Table>
<Country>China</Country>
<City>Shenzhen</City>
</Table>
<Table>
<Country>China</Country>
<City>Zhanjiang</City>
</Table>
<Table>
<Country>China</Country>
<City>Zhengzhou</City>
</Table>
<Table>
<Country>China</Country>
<City>Wuhan</City>
</Table>
<Table>
<Country>China</Country>
<City>Yinchuan</City>
</Table>
<Table>
<Country>China</Country>
<City>Jiuquan</City>
</Table>
<Table>
<Country>China</Country>
<City>Xi'An</City>
</Table>
<Table>
<Country>China</Country>
<City>Xining</City>
</Table>
<Table>
<Country>China</Country>
<City>Yan An</City>
</Table>
<Table>
<Country>China</Country>
<City>Kunming</City>
</Table>
<Table>
<Country>China</Country>
<City>Xiamen</City>
</Table>
<Table>
<Country>China</Country>
<City>Nanchang</City>
</Table>
<Table>
<Country>China</Country>
<City>Fuzhou</City>
</Table>
<Table>
<Country>China</Country>
<City>Ganzhou</City>
</Table>
<Table>
<Country>China</Country>
<City>Hangzhou</City>
</Table>
<Table>
<Country>China</Country>
<City>Nanjing</City>
</Table>
<Table>
<Country>China</Country>
<City>Hefei</City>
</Table>
<Table>
<Country>China</Country>
<City>Qingdao</City>
</Table>
<Table>
<Country>China</Country>
<City>Shanghai / Hongqiao</City>
</Table>
<Table>
<Country>China</Country>
<City>Jinan</City>
</Table>
<Table>
<Country>China</Country>
<City>Chongqing</City>
</Table>
<Table>
<Country>China</Country>
<City>Guiyang</City>
</Table>
<Table>
<Country>China</Country>
<City>Lhasa</City>
</Table>
<Table>
<Country>China</Country>
<City>Chengdu</City>
</Table>
<Table>
<Country>China</Country>
<City>Hami</City>
</Table>
<Table>
<Country>China</Country>
<City>Kashi</City>
</Table>
<Table>
<Country>China</Country>
<City>Hotan</City>
</Table>
<Table>
<Country>China</Country>
<City>Urum-Qi / Diwopu</City>
</Table>
<Table>
<Country>China</Country>
<City>Yining</City>
</Table>
<Table>
<Country>China</Country>
<City>Changchun</City>
</Table>
<Table>
<Country>China</Country>
<City>Qiqihar</City>
</Table>
<Table>
<Country>China</Country>
<City>Dalian</City>
</Table>
</NewDataSet>
{
String endpoint ="http://www.wopos.com/webservice/Weather.asmx?wsdl";
Service service = new Service();
Call call = (Call) service.createCall();call.setTargetEndpointAddress( new java.net.URL(endpoint) );
call.setOperationName(new QName("http://tempuri.org/", "getWeather")); //肯定是错的。call.setUseSOAPAction(true);
call.setSOAPActionURI("http://tempuri.org/getWeather");//肯定是错的。String ret = (String) call.invoke( new Object[] { "武汉" } );//用拼音。System.out.println("Sent ’Hello!’, got ’" + ret + "’"); //sent的是wuhuan
} catch (Exception e) {
System.out.println(e.toString());
}
http://blog.csdn.net/idilent/archive/2007/10/22/1836694.aspx
Service Definition 选择
WSDL URL:http://www.webservicex.net/globalweather.asmx?WSDL
这个在新建项目时,是不是只能填本地的,不能填网络上的啊,我的填了http://www.webservicex.net/globalweather.asmx?WSDL就不能进行下一步了!