Java 调用天气 WebService 的小应用
废话不多说 ، : :
CityReq.Java
package com.weather ؛ import javax.xml.bind.annotation.xmlelement ؛ import javax.xml.bind.annotation.xmlrootelement ؛ xmlrootelement (name = "getweatherbycityname" ، namespace = "http://webxml.com.cn/") public. السلسلة العامة getTheCityName () {return theCityName ؛ } xmlelement (name = "thecityName" ، namepace = "http://webxml.com.cn/") public void setTheCityName (String theCityName) {this.theCityName = theCityName ؛ }}WeatherWebservicetest.java
package com.weather ؛ import java.io.inputstream ؛ import java.io.outputstream ؛ import java.net.httpurlconnection ؛ import java.net.url ؛ import javax.xml.bind.jaxbcontext javax.xml.parsers.documentbuilderfactory ؛ import javax.xml.soap.messagefactory ؛ import javax.xml.soap.soapbody ؛ org.w3c.dom.document ؛ public class WeatherWebServicEtest {public static void main (string [] args) {// todo method method method method tuto weather () ؛ } static void weather () {system.out.println ("开始登陆 ...") ؛ String wsdl = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx؟wsdl" ؛ System.out.println ("WSDL:"+WSDL) ؛ httpurlConnection urlConn = null ؛ inputStream ins = null ؛ OutputStream ous = null ؛ حاول {url u = url new (wsdl) ؛ urlconn = (httpurlConnection) U.OpenConnection () ؛ urlconn.setDooutput (صواب) ؛ urlconn.setRequestMethod ("post") ؛ urlconn.setRequestProperty ("نوع المحتوى" ، "التطبيق/الصابون+xml ؛ charset = utf-8") ؛ //urlconn.setRequestProperty("content-type "،" text/xml ؛ charset = utf-8 ") ؛ // 发送数据 ous = urlconn.getOutputStream () ؛ وثيقة المستند = documentBuilderFactory.newinstance (). NewDocumentBuilder (). NewDocument () ؛ // 编组 Marshaller Marsh = JaxbContext.newinstance (CityReq.Class) .Createmarshaller () ؛ CityReq XMLF = New CityReq () ؛ xmlf.setTheCityName ("北京") ؛ //jaxb.marshal(xmlf ، new printWriter (system.out)) ؛ Marsh.Marshal (XMLF ، document) ؛ // 创建 soapmessage 对象 soapmessage soapmessage = messagefactory.newinstance (soapconstants.soap_1_2_protocol) .createmessage () ؛ soapbody soapbody = soapmessage.getSoapBody () ؛ soapbody.adddocument (وثيقة) ؛ soapenvelope soapenvelope = soapmessage.getSoappart (). getenvelope () ؛ soapenvelope.removenamespacedeclaration ("ENV") ؛ soapenvelope.addnamespacedeclaration ("SOAP12" ، "http://www.w3.org/2003/05/soap-envelope") ؛ soapenvelope.addnamespacedeclaration ("XSI" ، "http://www.w3.org/2001/xmlschema-instance") ؛ soapenvelope.addnamespacedeclaration ("XSD" ، "http://www.w3.org/2001/xmlschema") ؛ soapenvelope.setprefix ("SOAP12") ؛ soapenvelope.removechild (soapenvelope.getheader ()) ؛ soapbody.setprefix ("soap12") ؛ // 发送数据 soapmessage.writeto (OUS) ؛ // soapmessage.writeto (system.out) ؛ system.out.println (urlConn.getResponsecode ()) ؛ System.out.println (urlConn.getResponsemessage ()) ؛ // 接收数据 ins = urlconn.getInputStream () ؛ // 接收的数据需要解组؟ StringBuffer repsmsg = new StringBuffer () ؛ بايت [] بايت = بايت جديد [1024*1024] ؛ int a = -1 ؛ بينما ((a = ins.read (bytes))! =-1) {respmsg.append (سلسلة جديدة (بايت ، 0 ، أ)) ؛ } system.out.println (repsmsg.length ()) ؛ system.out.println (repsmsg) ؛ // 解组的方式 /* soapmessage reviewessage = messagefactory.newinstance (soapconstants.soap_1_2_protocol) .CreateMessage (NULL ، INS) ؛ unmarshaller unmarsh = jaxbcontext.newinstance (CityResp.Class) .CreateNmarshaller () ؛ jaxbelement <CityResp> Response = unmarsh.unmarshal (roseseMessage.getSoapbody (). extractContentAsdocument () ، cityresp.class) ؛ CityResp uResp = response.getValue () ؛ system.out.println (uresp.getResult ()) ؛*/ ous.close () ؛ ins.close () ؛ urlconn.disconnect () ؛ } catch (استثناء e) {E.PrintStackTrace () ؛ }أخيراً{ } } }感谢阅读 , 希望能帮助到大家 , 谢谢大家对本站的支持!