package com.citycollege.stw.weathertest;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
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 WeatherReport {
/**
* 15.* 獲取SOAP的請求頭,並替換其中的標誌符號爲用戶輸入的城市
*
* @param city
* 用戶輸入的城市名稱
* @return 客戶將要發送給服務器的SOAP請求
*/
private static String getSoapRequest(String city) {
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=/"1.0/" encoding=/"utf-8/"?>"
+ "<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//">"
+ "<soap:Body> <getWeatherbyCityName xmlns=/"http://WebXml.com.cn//">"
+ "<theCityName>" + city
+ "</theCityName> </getWeatherbyCityName>"
+ "</soap:Body></soap:Envelope>");
return sb.toString();
}
/**
* 用戶把SOAP請求發送給服務器端,並返回服務器點返回的輸入流
*
* @param city
* 用戶輸入的城市名稱
* @return 服務器端返回的輸入流,供客戶端讀取
* @throws Exception
*
*/
private static InputStream getSoapInputStream(String city) throws Exception {
try {
String soap = getSoapRequest(city);
if (soap == null) {
return null;
}
URL url = new URL(
"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Length", Integer.toString(soap
.length()));
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("SOAPAction",
"http://WebXml.com.cn/getWeatherbyCityName");
OutputStream os = conn.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
osw.write(soap);
osw.flush();
osw.close();
InputStream is = conn.getInputStream();
return is;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String getWeather(String city) {
try {
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream(city);
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName("string");
StringBuffer sb = new StringBuffer();
for (int count = 0; count < nl.getLength(); count++) {
Node n = nl.item(count);
if (n.getFirstChild().getNodeValue().equals("查詢結果爲空!")) {
sb = new StringBuffer("#");
break;
}
sb.append(n.getFirstChild().getNodeValue() + "#/n");
}
is.close();
System.out.println(sb.toString());
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return "發生異常未能獲取城市溫度";
}
}
private static String getTodayeWeather() {
String weather = "";
String city = "武漢";//web中應該根據用戶的輸入獲得城市
StringBuffer sb = new StringBuffer();
try {
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is =getSoapInputStream(city);
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName("string");
for (int count = 0; count < nl.getLength(); count++) {
Node n = nl.item(count);
if (n.getFirstChild().getNodeValue().equals("查詢結果爲空!")) {
sb = new StringBuffer("#");
break;
}
sb.append(n.getFirstChild().getNodeValue() + "#/n");
}
is.close();
} catch (Exception e) {
e.printStackTrace();
return "發生異常未能獲取城市溫度";
}
String wearths = sb.toString();
String[] weath = wearths.split("#");
for (int c=0;c<weath.length;c++){
System.out.println(c+":"+weath[c].trim());
}
String thisweath = weath[6];
String[] thisweat = thisweath.split(" ");
weather = thisweat[1] + " " + weath[5];
return weather;
}
private static List<String> getThreeDaysWeather(String city) {
String weather1 = "";
String weather2="";
String weather3="";
List<String>all=new ArrayList<String>();
List<String>allerr=new ArrayList<String>();
allerr.add("發生異常未能獲取城市溫度");
//String city = "武漢";//web中應該根據用戶的輸入獲得城市
StringBuffer sb = new StringBuffer();
try {
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is =getSoapInputStream(city);
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName("string");
for (int count = 0; count < nl.getLength(); count++) {
Node n = nl.item(count);
if (n.getFirstChild().getNodeValue().equals("查詢結果爲空!")) {
sb = new StringBuffer("#");
break;
}
sb.append(n.getFirstChild().getNodeValue() + "#/n");
}
is.close();
} catch (Exception e) {
e.printStackTrace();
return allerr;
}
String wearths = sb.toString();
String[] weath = wearths.split("#");
//for (int c=0;c<weath.length;c++){
// System.out.println(c+":"+weath[c].trim());
// }
System.out.println(weath.length);
String thisweath = weath[6];
String[] thisweat = thisweath.split(" ");
weather1 = thisweat[1] + " " + weath[5];
weather1="今天"+" "+weather1;
all.add(weather1);
// String thisweath2 = weath[12];
// String[] thisweat2 = thisweath2.split(" ");
// weather2 = thisweat2[1] + " " + weath[11];
weather2=weath[13]+" "+weath[12];
all.add(weather2);
//for(int i=5;i<=6;i++){
//System.out.println(weath[17]);
//}
weather3=weath[18]+" "+weath[17];
all.add(weather3);
return all;
}
/**
* 測試用
* 在web項目中可以直接調用其中的方法,直接得到某地當天或未來兩天的天氣狀態
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//System.out.println(getTodayeWeather());
//getTodayeWeather();
try {
List<String>all=new ArrayList<String>();
all=getThreeDaysWeather("汕頭");
for(String a:all){
System.out.println(a);
}
} catch (Exception e) {
//e.printStackTrace();
System.out.println("網絡異常");
}
// System.out.println("po&oi".split("&").length) ;
// System.out.println("##".split("#").length) ;
}
}