package com.lwz.wx.util;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class Gettqbycs {
public String palceRequestUrl(String city) throws UnsupportedEncodingException {
//String url="http://xml.weather.yahoo.com/forecastrss?u=c&p=CHXX"+city;
String url="http://php.weather.sina.com.cn/xml.php?city="+city+"&password=DJOYnieT8234jlsK&day=0"; //這里的0 表示今天 要查明天換成 1
return url;
}
public String Gettqbycs(String csname) throws UnsupportedEncodingException{
String url = palceRequestUrl(csname);
System.out.println(url);
String returnStr="";
HttpClient client = new HttpClient();
PostMethod getMethod = new PostMethod(url);
try {
client.executeMethod(getMethod);
returnStr=new String(getMethod.getResponseBodyAsString().getBytes("iso-8859-1"),"UTF-8");//鏈接返回來(lái)的信息
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//JSONObject jsonObj = JSONObject.fromObject(returnStr );
String s=returnStr;
// System.out.println(s);
return s;
};
public String readStringXml(String xml) {
StringBuffer buff=new StringBuffer();
Document doc = null;
try {
// 讀取并解析XML文檔
// SAXReader就是一個(gè)管道,用一個(gè)流的方式,把xml文件讀出來(lái)
//
// SAXReader reader = new SAXReader(); //User.hbm.xml表示你要解析的xml文檔
// Document document = reader.read(new File("User.hbm.xml"));
// 下面的是通過(guò)解析xml字符串的
doc = DocumentHelper.parseText(xml); // 將字符串轉(zhuǎn)為XML
Element rootElt = doc.getRootElement(); // 獲取根節(jié)點(diǎn)
System.out.println("根節(jié)點(diǎn):" + rootElt.getName()); // 拿到根節(jié)點(diǎn)的名稱
Iterator iter = rootElt.elementIterator("Weather"); // 獲取根節(jié)點(diǎn)下的子節(jié)點(diǎn)Weather
// 遍歷head節(jié)點(diǎn)
while (iter.hasNext()) {
Element recordEle = (Element) iter.next();
String city = recordEle.elementTextTrim("city"); // 拿到Weather節(jié)點(diǎn)下的子節(jié)點(diǎn)title值
buff.append("您好,我是小V,以下是今天的天氣情況!").append("\n\n");
buff.append("城市:"+city).append("\n");
String status1=recordEle.elementTextTrim("status1");
String status2=recordEle.elementTextTrim("status2");
buff.append("天氣情況:"+status1+"轉(zhuǎn)"+status2).append("\n");
String temperature1=recordEle.elementTextTrim("temperature1");
String temperature2=recordEle.elementTextTrim("temperature2");
buff.append("溫度:"+temperature1+"~~"+temperature2+"攝氏度").append("\n");
String chy_shuoming=recordEle.elementTextTrim("chy_shuoming");
buff.append("建議穿衣:"+chy_shuoming).append("\n");
String gm_s=recordEle.elementTextTrim("gm_s");
buff.append("需要注意:"+gm_s).append("\n");
buff.append("很高興為您服務(wù)").append("\n");
buff.append("回復(fù) ? 顯示需要的幫助。").append("\n\n");;
}
} catch (DocumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return buff.toString();
}
public static void main(String[] args) {
Gettqbycs test=new Gettqbycs();
try {
String instr="福州天氣"; //這個(gè)是個(gè)人其他項(xiàng)目測(cè)試,可以自己處理下
System.out.println(instr.substring(2, 4));
String city=instr.substring(0, 2);
String ss= test.Gettqbycs(URLEncoder.encode(city, "GBK"));
String buffstr=test.readStringXml(ss);
System.out.println(buffstr);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。