axis WebServices 完美呼叫天氣預報,查詢、顯示 程式碼!
axis WebServices 完美呼叫天氣預報,查詢、顯示 程式碼!
效果:
jsp頁面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>城市天氣資訊</title>
</head>
<style>
body{
font-size:12px;
}
table{
border-collapse:sparate;
border-spacing:0px;
}
td{
padding:0px;
border:0px solid #000;
text-align:center;
font-size:12px;
color:#2A5CAA;
border-color:#2A5CAA;
}
.noMess{
text-align: center;
text-valign: center;
}
</style>
<body>
<form action="webservice.action" method="post" name="form1">
<input type="text" name="city" /> <input type="submit" value="查詢" id="sub"/>
</form>
<br/>
<!-- 顯示天氣 -->
<c:if test="${not empty weathers}">
<table border="0" cellpadding="0" cellspacing="0" width="380">
<tr>
<td>${weathers._regionFirst} ${weathers._regionSecond} 72小時天氣預報</td>
<td>最新上報時間:${weathers._reportTime}</td>
</tr>
<tr>
<td colspan="2">
<!-- 今天天氣 -->
<table border="0" cellpadding="0" cellspacing="0">
<tr><td>
${weathers._todayDetail}
</td></tr>
<tr>
<td>${weathers._todayDate} ${weathers._todayTemperature}</td>
</tr>
<tr>
<td> ${weathers._todayWind}</td>
</tr>
<tr>
<td>
<img src="./images/weather/${weathers._todayPic_1}"/>
<img src="./images/weather/${weathers._todayPic_2}">
</td>
</tr>
<tr>
<td></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">
<!-- 明天天氣 -->
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>${weathers._tomorrowDate} ${weathers._tomorrowTemperature}</td>
</tr>
<tr>
<td>${weathers._tomorrowWind}</td>
</tr>
<tr>
<td>
<img src="./images/weather/${weathers._tomorrowPic_1}"/>
<img src="./images/weather/${weathers._tomorrowPic_2}">
</td>
</tr>
<tr>
<td></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">
<!-- 後天天氣 -->
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>${weathers._affterTomorrowDate} ${weathers._affterTomorrowTemperature}</td>
</tr>
<tr>
<td>${weathers._affterTomorrowWind}</td>
</tr>
<tr>
<td>
<img src="./images/weather/${weathers._affterTomorrowPic_1}"/>
<img src="./images/weather/${weathers._affterTomorrowPic_2}">
</td>
</tr>
<tr>
<td></td>
</tr>
</table>
</td>
</tr>
</table>
</c:if>
<!-- 自動載入 ajax 方式 等寫
<script type="text/javascript">
window.onload= function(){
//location.href=form1.submit();
document.getElementById("sub").click();
};
</script>
-->
</body>
</html>
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="webservice" class="accp.action.WebservicesAction">
<result name="success">
index.jsp
</result>
</action>
</package>
</struts>
action程式碼:
package accp.action;
import java.util.Hashtable;
import accp.util.WeatherUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class WebservicesAction extends ActionSupport {
private String city;
//存放天氣資訊
Hashtable<String,Object> weathers=new Hashtable<String,Object>();
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String execute() throws Exception {
System.out.println("-----開始查詢-------");
if("".equals(city)||null==city){
city="廣州";
}
//得到天氣資訊
weathers=WeatherUtil.getInstancce().getWea(city);
ActionContext.getContext().put("weathers", weathers);
return "success";
}
}
實現類程式碼:
package accp.util;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Hashtable;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class WeatherUtil {
// 編碼
private String enCoding ="utf-8";
// 目標url
private String targetWebserviceUri="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
//private String targetWebserviceUri = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl";
private WeatherUtil(){};
public static WeatherUtil getInstancce(){
return new WeatherUtil();
}
/**
* 對伺服器端返回的XML進行解析
* WeatherObj 常量
* @param city 使用者輸入的城市名稱
* @return 字串 用,分割
* @throws Exception
*/
public Hashtable<String,Object> getWea(String city) throws Exception{
Hashtable<String,Object> weathers=new Hashtable<String,Object>();
Document doc=null;
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
InputStream is=getResponseSOAP(city);//獲取流
doc=db.parse(is); //獲取 xml文件
NodeList nl=doc.getElementsByTagName("string");//獲取所有節點
//迴圈讀取 並放入hashtable
System.out.println("元素Length:"+nl.getLength());
for(int i =0;i<nl.getLength();i++){
Node n = nl.item(i);
String nodeValue=n.getFirstChild().getNodeValue();
if(i !=11){
System.out.println(nodeValue);
weathers.put(WeatherObj.input.get(i), nodeValue) ;
}
}
return weathers;
}
/**
* 封裝xml
*/
public String getRequestSOAP(String city){
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
sb.append("<soap:Body>");
sb.append("<getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">");
sb.append(" <theCityName>"+city+"</theCityName>");
sb.append("</getWeatherbyCityName>");
sb.append("</soap:Body>");
sb.append("</soap:Envelope>");
return sb.toString();
}
/**
* @throws MalformedURLException
* 獲得流
*/
private InputStream getResponseSOAP(String city) throws Exception{
String requestSOAP=getRequestSOAP(city);
// 建立url
URL url= new URL(targetWebserviceUri);
//得到url 的連結
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
// 設定請求的頭資訊
conn.setRequestProperty("Content-Type", "text/xml; charset="+enCoding);
conn.setRequestProperty("Content-Length", requestSOAP.length()+"");
conn.setRequestProperty("SOAPAction", "http://WebXml.com.cn/getWeatherbyCityName");
OutputStream os = conn.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, enCoding);
osw.write(requestSOAP);
osw.flush();
osw.close();
return conn.getInputStream() ;
}
}
hashtable 類程式碼
package accp.util;
import java.util.Hashtable;
/**
* @author hxb
*
*/
public class WeatherObj {
public static final String REGIONFIRST = "_regionFirst";//上級行政區
public static final String REGIONSECOND = "_regionSecond";//本級行政區
public static final String REGIONID = "_regionId";//行政區id
public static final String REGIONPIC = "_regionPic";//行政區圖片
public static final String REPORTTIME = "_reportTime";//最新上報時間
public static final String TODAYTEMPERATURE = "_todayTemperature";//今天溫度
public static final String TODAYDATE = "_todayDate";//今天日期
public static final String TODAYWIND = "_todayWind";//今天風況
public static final String TODAYPIC_1 = "_todayPic_1";//今天天氣圖片1
public static final String TODAYPIC_2 = "_todayPic_2";//今天天氣圖片2
public static final String TODAYDETAIL = "_todayDetail";//今天天氣實況
public static final String ZHISHU = "_zhiShu";//各個指數
/*public static final String CHUANYIZHISHU = "穿衣指數:";
public static final String GANMAOZHISHU = "感冒指數:";
public static final String CHENLIANZHISHU = "晨練指數:";
public static final String JIAOTONGZHISHU = "交通指數:";
public static final String LIANGSHAIZHISHU = "晾曬指數:";
public static final String LVYOUZHISHU = "旅遊指數:";
public static final String LUKUANGZHISHU = "路況指數:";
public static final String SHUSHIDUZHISHU = "舒適度指數:"; */
/*
public static final String CHUANYIZHISHU = "穿衣指數:";
public static final String GUOMINZHISHU = "過敏指數:";
public static final String YUANDONGZHISHU = "運動指數:";
public static final String LIANGSHAIZHISHU = "晾曬指數:";
public static final String LVYOUZHISHU = "旅遊指數:";
public static final String LUKUANGZHISHU = "路況指數:";
public static final String SHUSHIDUZHISHU = "舒適度指數:";
public static final String KONGQIWURANGZHISHU="空氣汙染指數:";
*/
public static final String TOMORROWTEMPERATURE = "_tomorrowTemperature";//明天溫度
public static final String TOMORROWDATE = "_tomorrowDate";//明天日期
public static final String TOMORROWWIND = "_tomorrowWind";//明天風況
public static final String TOMORROWPIC_1 = "_tomorrowPic_1";//明天天氣圖片1
public static final String TOMORROWPIC_2 = "_tomorrowPic_2";//明天天氣圖片2
public static final String AFTERTOMORROWTEMPERATURE = "_affterTomorrowTemperature";//後天溫度
public static final String AFTERTOMORROWDATE = "_affterTomorrowDate";//後天日期
public static final String AFTERTOMORROWWIND = "_affterTomorrowWind";//後天風況
public static final String AFTERTOMORROWPIC_1 = "_affterTomorrowPic_1";//後天天氣圖片1
public static final String AFTERTOMORROWPIC_2 = "_affterTomorrowPic_2";//後天天氣圖片2
public static final String DESCRIPT = "_descript";//本地介紹
public static final Hashtable<Integer,String> input = new Hashtable<Integer,String>(0);//常量與數量對應
static{
input.put(0, REGIONFIRST);
input.put(1, REGIONSECOND);
input.put(2, REGIONID);
input.put(3, REGIONPIC);
input.put(4, REPORTTIME);
input.put(5, TODAYTEMPERATURE);
input.put(6, TODAYDATE);
input.put(7, TODAYWIND);
input.put(8, TODAYPIC_1);
input.put(9, TODAYPIC_2);
input.put(10, TODAYDETAIL);
input.put(11, ZHISHU);
input.put(12, TOMORROWTEMPERATURE);
input.put(13, TOMORROWDATE);
input.put(14, TOMORROWWIND);
input.put(15, TOMORROWPIC_1);
input.put(16, TOMORROWPIC_2);
input.put(17, AFTERTOMORROWTEMPERATURE);
input.put(18, AFTERTOMORROWDATE);
input.put(19, AFTERTOMORROWWIND);
input.put(20, AFTERTOMORROWPIC_1);
input.put(21, AFTERTOMORROWPIC_2);
input.put(22, DESCRIPT);
}
private String name;
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
相關文章
- 查詢天氣預報網站網站
- 【吐血整理】微信小程式如何接入天氣預報查詢 API微信小程式API
- 天氣預報查詢 API + AI 等於王炸(一大波天氣預報查詢 API 應用場景更新了)APIAI
- 使用 Wttr.in 在你的終端中顯示天氣預報
- 天氣預報查詢 API 提供個性化的天氣服務的設計思路API
- python天氣查詢Python
- hive表查詢中文顯示亂碼Hive
- 全國天氣預報資訊資料 API 功能簡介與程式碼呼叫實戰視訊API
- react native天氣預報React Native
- flutter天氣預報APPFlutterAPP
- 天氣預報API介面API
- springboot應用查詢城市天氣Spring Boot
- 為什麼win10天氣始終顯示北京_win10系統的天氣預報怎麼設定Win10
- vue例項+axios-天氣查詢VueiOS
- Flutter實踐:天氣預報Flutter
- 各種免費好用的api,含天氣查詢、IP查詢、物流查詢等API
- 命令列查詢天氣的正確方式命令列
- 0828-T3 天氣預報
- 5.22 天氣預報系統 小
- Python 獲取當地未來五天天氣 天氣預報 獲取天氣Python
- 請利用SAX編寫程式解析Yahoo的XML格式的天氣預報,獲取天氣預報——python學習筆記XMLPython筆記
- Python實現天氣查詢功能(外加Excel技巧)PythonExcel
- [TJOI2010] 天氣預報 題解
- 報表的查詢皮膚怎麼顯示在左側?
- 天氣預報戰略升級為“新晴天氣”,深耕天氣+出行生活場景
- win10系統下天氣磁貼不顯示當地天氣如何解決Win10
- [Python實戰]Python製作天氣查詢軟體Python
- 通過cobra開發天氣查詢小工具
- ES查詢之查詢屬性過濾、結果高亮顯示
- Mac天氣預報元件:Weather Widget Live for MacMac元件
- 天氣預報:2020年春節出行指南
- 基於Qt的天氣預報專案QT
- 天氣預報API,你想要的它都有API
- 天氣預報到底有什麼作用?
- w10系統天氣為什麼只顯示北京天氣_win10系統本地天氣如何設定Win10
- 天氣預報更名“新晴天氣”,品牌升級助力智慧生活
- 天氣出行、手機號查詢、簡訊、IP查詢類好用的介面推薦
- Flutter實戰1 --- 寫一個天氣查詢的APPFlutterAPP
- Flutter實戰2 — 寫一個天氣查詢的APPFlutterAPP