天天看点

webservice之获取天气预报

wsdl地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?op=getWeatherbyCityName

soap:

SOAP 1.1

以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

POST /WebServices/WeatherWebService.asmx HTTP/1.1
Host: www.webxml.com.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://WebXml.com.cn/getWeatherbyCityName"

<?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>string</theCityName>
    </getWeatherbyCityName>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?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>
    <getWeatherbyCityNameResponse xmlns="http://WebXml.com.cn/">
      <getWeatherbyCityNameResult>
        <string>string</string>
        <string>string</string>
      </getWeatherbyCityNameResult>
    </getWeatherbyCityNameResponse>
  </soap:Body>
</soap:Envelope>
SOAP 1.2

以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

POST /WebServices/WeatherWebService.asmx HTTP/1.1
Host: www.webxml.com.cn
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <getWeatherbyCityName xmlns="http://WebXml.com.cn/">
      <theCityName>string</theCityName>
    </getWeatherbyCityName>
  </soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <getWeatherbyCityNameResponse xmlns="http://WebXml.com.cn/">
      <getWeatherbyCityNameResult>
        <string>string</string>
        <string>string</string>
      </getWeatherbyCityNameResult>
    </getWeatherbyCityNameResponse>
  </soap12:Body>
</soap12:Envelope>
           

代码如下:

private static void getWeatherbyCityName(String theCityName)
			throws XMLStreamException {
		try {
			String soapBindingAddress = "http://webservice.webxml.com.cn/webservices/WeatherWebService.asmx";
			ServiceClient sender = new ServiceClient();
			EndpointReference endpointReference = new EndpointReference(
					soapBindingAddress);
			Options options = new Options();
			options.setAction("http://WebXml.com.cn/getWeatherbyCityName");
			options.setTo(endpointReference);
			sender.setOptions(options);
			OMFactory fac = OMAbstractFactory.getOMFactory();
			OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/",
					"getWeatherbyCityName");
			OMElement data = fac.createOMElement("getWeatherbyCityName", omNs);
			String[] strs = new String[] { "theCityName" };
			String[] val = new String[] { theCityName };
			for (int i = 0; i < strs.length; i++) {
				OMElement inner = fac.createOMElement(strs[i], omNs);
				inner.setText(val[i]);
				data.addChild(inner);
			}
			OMElement result = sender.sendReceive(data);
			System.out.println(result.toStringWithConsume());
		} catch (AxisFault ex) {
			ex.printStackTrace();
		}

	}
           

调用:getWeatherbyCityName("南京");

结果返回:

<getWeatherbyCityNameResponse xmlns="http://WebXml.com.cn/"><getWeatherbyCityNameResult><string>江苏</string><string>南京</string><string>58238</string><string>58238.jpg</string><string>2014-1-7 15:42:03</string><string>6℃/9℃</string><string>1月7日 小雨</string><string>东北风3-4级转西北风4-5级</string><string>7.gif</string><string>7.gif</string><string>今日天气实况:气温:8℃;风向/风力:北风 2级;湿度:93%;空气质量:暂无;紫外线强度:最弱</string><string>穿衣指数:冷,建议着棉衣加羊毛衫等冬季服装。
过敏指数:极不易发,无需担心过敏,可放心外出,享受生活。
运动指数:较不宜,有降水,推荐您在室内进行休闲运动。
洗车指数:不宜,有雨,雨水和泥水会弄脏爱车。
晾晒指数:不宜,降水可能会淋湿衣物,请选择在室内晾晒。
旅游指数:一般,风大天凉注意保暖,有降水需带雨具。
路况指数:潮湿,有降水,路面潮湿,请小心驾驶。
舒适度指数:较舒适,白天降雨,有些凉意。
空气污染指数:暂无。
紫外线指数:最弱,辐射弱,涂擦SPF8-12防晒护肤品。</string><string>-1℃/6℃</string><string>1月8日 小雨转多云</string><string>北风5-6级转东北风4-5级</string><string>7.gif</string><string>1.gif</string><string>-3℃/5℃</string><string>1月9日 晴</string><string>东风3-4级</string><string>0.gif</string><string>0.gif</string><string>南京简称“宁”,别名“金陵”,也曾称建业、建康、石头城等,现为江苏省省会,是长江下游西部的中心城市。它位于江苏省西部,东依宁镇山脉,地势险固,风景秀丽。诸葛亮曾对南京一带的山川形势评价说:“钟阜龙蟠,石城虎踞”。南京属北亚热带季风气候区,四季分明,年度最佳气节为秋季(9-11月)。南京是历经苍桑的十代都会。三国鼎立,她目睹群雄角逐争战;六代兴替,她阅尽王朝的曲终幕落;明初,她以举世无双的巍巍城垣显示了泱泱大国之风;晚清,她为近代中国第一个不平等条约被冠上自己的名字而蒙受辱;太平天国,历史在这里风雷激荡;辛亥革命,潮流在这里奔突迂回;抗日战争,日军在这里留下人类历史上最野蛮、最血腥的一页。景观:南京秦淮河,中山陵,玄武湖,莫愁湖,雨花台景区,明孝陵,栖霞山,南京长江大桥。南京是中国的历史文化名城之一,文化古迹比较集中,有新石器时代古文化遗址多处,有三国东吴所筑石头城遗址、南京帝王的陵墓、明代朱元璋的陵墓(明孝陵等)。名胜游览地也很多,主要有中山陵、玄武湖、灵谷寺、秦淮河和栖霞山等。革命纪念地有梅园新村、雨花台等。钟山风景区为国家第一批国家重点风景名胜区,位于南京东北郊,以钟山和玄武湖为中心,是来南京旅游的旅游者的必游之地。这一带主要景观包括中山陵、明孝陵、孙权墓和灵谷寺等。南京山、水、城、林相映成趣,景色壮丽秀美,是中国著名的风景旅游城市。</string></getWeatherbyCityNameResult></getWeatherbyCityNameResponse>
           

继续阅读