学习Webservice之入天气小试
主要方法是:通过程序中设置代理用公司内网访问外部Webservice
public InputStream getSoapInputStream(String url) {
InputStream inputStream = null;
try {
URL urlObj = new URL(url);
InetSocketAddress addr = new InetSocketAddress("dailifuwuqiIP", duankou);//此处为代理服务器地址,端口
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
HttpURLConnection urlConn = (HttpURLConnection) urlObj
.openConnection(proxy);
urlConn.setRequestProperty("Host", SERVICES_HOST); // 具体webService相关
urlConn.setRequestMethod("GET");
urlConn.connect();
inputStream = urlConn.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return inputStream;
}
类文件:
Weather.java package weather; import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException; public class weather {
// private static String SERVICES_HOST = "www.webxml.com.cn";
private static String SERVICES_HOST = "webservice.webxml.com.cn"; /**
* 城市代码 / 浦东: 2015
*/
private static int CITICODE = ; public static void main(String[] args) throws Exception {
String time=null;
Calendar cal=Calendar.getInstance();
SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd");//MM要大写
time=format.format(cal.getTime()); String desc = "今天是" + time+",浦东";
desc += new weather().getWeatherStr();
System.out.println(desc);
} /**
* Sos,MMyTest
* */
public InputStream getSoapInputStream(String url) {
InputStream inputStream = null;
try {
URL urlObj = new URL(url);
InetSocketAddress addr = new InetSocketAddress("XXX.XX.XX.XX", XXXX);//此处为代理服务器,和端口
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
HttpURLConnection urlConn = (HttpURLConnection) urlObj
.openConnection(proxy);
urlConn.setRequestProperty("Host", SERVICES_HOST); // 具体webService相关
urlConn.setRequestMethod("GET");
urlConn.connect();
inputStream = urlConn.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return inputStream;
} public String getWeatherStr() {
String desc = "";
try {
List<String> weatherList = getWeather(CITICODE); if (weatherList != null && weatherList.size() > ) {
String tianqi = weatherList.get();
if (tianqi.contains("日")) {
tianqi = tianqi.substring(tianqi.indexOf("日") + );
}
String wendu = weatherList.get();
desc += ",天气" + tianqi;
desc += " ,";
desc += wendu.replace("℃", "度").replace("/", "--");
}
} catch (Exception e) {
e.printStackTrace();
return desc;
}
return desc;
} public List<String> getWeather(int cityCode) {
List<String> weatherList = new ArrayList<String>();
Document document;
DocumentBuilderFactory documentBF = DocumentBuilderFactory
.newInstance();
documentBF.setNamespaceAware(true);
try {
DocumentBuilder documentB = documentBF.newDocumentBuilder();
InputStream inputStream = getSoapInputStream("http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather?theUserID=&theCityCode=2015");
document = documentB.parse(inputStream);
NodeList nl = document.getElementsByTagName("string");
int len = nl.getLength();
for (int i = ; i < len; i++) {
Node n = nl.item(i);
String weather = n.getFirstChild().getNodeValue();
weatherList.add(weather);
}
inputStream.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (DOMException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return weatherList;
}
}
运行weather.java文件后打印出:
今天是2014-04-30,浦东,天气 晴转多云 ,14度--24度
学习Webservice之入天气小试的更多相关文章
- Java中Webservice调用.NET天气接口生成客户端异常
学习webservice时候有个例子调用公网的天气预报接口实现查询天气的功能.然而在使用命令编译客户端代码的时候出错了.大概看了一下网上说是需要将将文件中所有出现的 < s:element re ...
- 学习 WebService 第三步:一个简单的实例(RAD+WAS 8.5开发SOAP项目)
[开发环境] Web Service 服务器端开发工具:RAD(Eclipse内核) Web Service 服务器:IBM WebSphere v8.5 REST/SOAP:SOAP(JAX-WS/ ...
- WebService初入
WebService 1. WebService基本认识 WebService最早是微软提出了一种以XML为载体网络信息传输的规范,现在几乎所有的语言与平台都支持,带有状态机制,不依赖于容器,可以发送 ...
- webserive学习记录4-获取天气的例子
学习到了如何创建使用webservice服务,下面就实际应用一下,从网络上获取天气数据. 先从网络上找到免费的webservice服务, 如这个网站:http://www.webxml.com.cn/ ...
- curl通过调用WebService查询当前天气
<?php /** * curl通过调用WebService查询北京的当前天气 */ header("Content-type: text/html; charset=utf-8&qu ...
- Spring AOP 自定义注解获取http接口及WebService接口入参和出参
注解方法实现过程中可以采用如下获取方式:—以下为例 HttpServletRequest request = ((ServletRequestAttributes) RequestContextHo ...
- 学习 WebService 第五步:在Local创建测试用WebService(WSDL)
[准备] Eclipse+Tomcat7(Tomcat端口修改为不冲突的值) axis2 1.7.7 jar包(没有来这里下载:http://www.apache.org/dyn/closer.lua ...
- c++学习笔记01-输入输出流&&文件输入输出操作
const int size=50等于#define size 50 字符串默认以0结尾: 一.输入输出流 cin.get()会从输入中取走一个字符: cin.peek()会从输入中检测第一个字符: ...
- C语言学习笔记frist---输入两个数比较大小
C#学习中,问道艰辛,今自C学起,第一个函数学习:输入两个数比较大小,仅作练习: #include "stdafx.h" #include<stdio.h> // 包含 ...
随机推荐
- 【spring】non-compatible bean definition of same name and class
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected excep tion parsing XML do ...
- wcf 获取客户端 IP
http://stackoverflow.com/questions/3937773/wcf-security-using-client-ip-address var context = Operat ...
- 使用laravel的Eloquent模型获取数据库的指定列
使用laravel的Eloquent模型获取数据库的指定列 使用Laravel的ORM——Eloquent时,时常遇到的一个操作是取模型中的其中一些属性,对应的就是在数据库中取表的特定列. 如果使 ...
- Python 处理多层嵌套列表
>>> movies =[ "the holy grail", 1975,"terry jones",91, ["graham ch ...
- NodeJS的异步编程风格
NodeJS的异步编程风格 http://www.infoq.com/cn/news/2011/09/nodejs-async-code NodeJS运行环境因其支持Javascript语言和异步编程 ...
- C语言每日一题之No.9
再做决定之前,我还是做好自己该做的.我不希望几年后会悔恨自己为什么在最该努力的时候不愿意吃苦.尊敬的女王陛下,请接题: 一.题目:有已按升序排好顺序的字符串a,编写程序将字符串s中的每个字符按升序的规 ...
- SVN switch 用法详解
一直知道SVN有个switch命令,但是对它的介绍教程却很少,大多是生硬的svn帮助文档里的文字,从而一直不怎么会用.今天看了这篇文章,突觉豁然开朗,整理下来以备查阅. 使用SVN,自然是需要与别人合 ...
- final specifier (since C++11)
Specifies that a virtual function cannot be overridden in a derived class or that a class cannot be ...
- setValue:forUndefinedKey
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewControlle ...
- JQ获取当前是第几个元素,以及直接选取第几个元素的方法
一.获取当前是第几个元素的方法使用:$(this).index() 实例: $(function () { $('.menu li').mouseover(function () { alert($( ...