公司要在CRM系统中调用天气预报
用JAVA写的系统
请问高手可以怎么实现???
用JAVA写的系统
请问高手可以怎么实现???
解决方案 »
- SSH框架整合问题
- ssh 如何去得当前在线人数?服务器为tomcat 或者 websphere
- 为什么使用xml?
- 基础问题:关于xml的element name命名规则问题
- 关于Lucene搜索结果的问题:IndexSearch与Hits
- 怪了,可save()但不可以 find()??
- 标签库高手请帮我看看,这个标签我错在哪里
- 100请求IBM Websphere Studio Device Developer (集成开发工具)
- !急啊 立刻给分!weblogic.ant.taskdefs.ejb.DDInit生成xml时出错信息!什么原因?
- solrcloud上传solrhome配置文件到zookeeper zookeeper里面却没有configs这个文件
- urlrewrite
- 如何固定查询结果列表的列名和列表下面的button
直接添加,读取gogle的的天气预报
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;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;import weather.logger.MyLogger;public class WeatherManager {
private static MyLogger logWriter = new MyLogger(WeatherManager.class.getName());
public static String GetWeather(String cityName) {
String w = null;// 公用w try {
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream(cityName);
if (is == null) {
return null;
}
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(); w = sb.toString(); } catch (Exception e) {
logWriter.error(e.fillInStackTrace());
}
return w;
} private static String getSoapRequest(String city) {
StringBuffer sb = new StringBuffer();
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();
} 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.setConnectTimeout(1000 * 30);
conn.setReadTimeout(1000 * 60);
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) {
logWriter.error(e);
return null;
}
}
}
WebService 还用得不太熟!呵呵