package myweather;
import java.io.*;
import java.net.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.swing.JOptionPane;public class MyWeather {
private static String getSoapRequest(String city) {
try {
InputStreamReader isr = new InputStreamReader(new FileInputStream("weather.xml"));
BufferedReader reader = new BufferedReader(isr);
String soap = "";
String tmp;
while ((tmp = reader.readLine()) != null) {
soap += tmp;
}
reader.close();
isr.close();
return soap.replace("${city}$", city);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
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(new String(soap.getBytes(),"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("getWeatherbyCityNameResult");
Node n=nl.item(0);
//JOptionPane.showMessageDialog(null,n.getChildNodes().getLength());
for (int i=0;i<n.getChildNodes().getLength()-1;i++)
{
String weather=n.getChildNodes().item(i).getTextContent();
System.out.print(weather);
}
is.close();
return "null";
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
public static void main (String [] args )throws Exception
{
//System.out.println(MyWeather.getWeather("上海"));
MyWeather.getWeather("广州");
}}
//将以下XML保存为weather.xml
/*XML:<?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>*/
智能天气查询 java java天气预报程序
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
使用豆包Marscode 创建了一个”天气预报“小应用
用豆包MarsCode 制作天气预报系统啦
开发者 数据 计算机 ai 人工智能 -
java 天气预报
package work;import java.awt.BorderLayout;import java.awt.GridLayout;impor
java 开发语言 后端 System ci -
java天气预报 java天气预报app
java天气预报_天气预报 - java代码库 - 云代码
java天气预报 Image xml css -
android结合和风天气实现天气预报 android天气预报程序源码
1.先看看布局,一个编辑框,一个按钮,一个表格布局<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
android结合和风天气实现天气预报 android string layout exception -
springboot调用c语言动态库
以下的内容参考了网络上的内容,在此对原作者表示感谢! Java中动态代理的实现,关键就是这两个东西:Proxy、InvocationHandler,下面从InvocationHandler接口中的invoke方法入手,简单说明一下Java如何实现动态代理的。 &
springboot调用c语言动态库 spring 动态代理 System Java