Java 调用天气 Webservicio 的小应用
废话不多说, 直接贴代码 :
Cityreq.java
paquete com.weather; import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmlrooTelement; @xmlrootelement (name = "getweatherbycityName", namespace = "http://webxml.com.cn/") Class pública CityRq {private Sing thecity public String getTheCityName () {return theCityName; } @Xmlelement (name = "thEcityName", namespace = "http://webxml.com.cn/") public void setTheCityName (String thecityName) {this.thecityName = thecityName; }}WeatherWebServiceTest.java
paquete com.weather; import java.io.inputstream; import java.io.outputstream; import java.net.httpurlconnection; import java.net.url; import javax.xml.bind.jaxbcontext; import javax.xml.bind.marshaller; import javax.xml.pars.pars.parsers. javax.xml.soap.messageFactory; import javax.xml.soap.soapbody; import javax.xml.soap.soapconstants; import javax.xml.soap.soapenvelope; import javax.xml.soap.soapmessage; import og.w3c.c.dom.document; weatment; {public static void main (string [] args) {// tODO método generado automático stub 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; intente {url u = new url (wsdl); urlconn = (httpurlconnection) u.openconnection (); urlconn.setDoOutput (verdadero); urlconn.setRequestMethod ("post"); urlconn.setRequestProperty ("Content-type", "Aplicación/SOAP+XML; Charset = UTF-8"); //urlconn.setRequestProperty("Content-type "," Text/xml; Charset = UTF-8 "); // 发送数据 ous = urlconn.getOutputStream (); Documento documento = 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, documento); // 创建 SOAPMessage 对象 SOAPMessage SoapMessage = MessageFactory.Newinstance (SoapConstants.SOAP_1_2_Protocol) .CreateMessage (); Soapbody Soapbody = SoapMessage.getSoApbody (); Soapbody.addDocument (documento); SOAPENVELOPE SOAPENVELOPE = SOAPMESSAGE.GETSOAPSET (). GetEnvelope (); SOAPENVELOPE.REMOVERASPACECECECECECTARACIÓN ("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 respmsg = new StringBuffer (); byte [] bytes = new Byte [1024*1024]; int a = -1; while ((a = ins.read (bytes))! =-1) {respmsg.append (nueva cadena (bytes, 0, a)); } System.out.println (respmsg.length ()); System.out.println (Respmsg); // 解组的方式 /* SOAPMessage ResponseMessage = MessageFactory.newinstance (SoapConstants.SOAP_1_2_Protocol) .CreateMessage (null, ins); Unmarshaller unmarsh = jaxbcontext.newinstance (CityResp.Class) .CreateUnmarShaller (); Jaxbelement <cityResp> Reponse = unmarsh.unmarshal (ResponseMessage.getSoApbody (). ExtractContentesDocument (), CityResp.Class); CityResp uresp = Reponse.getValue (); System.out.println (uresp.getResult ());*/ oussclose (); ins.close (); urlconn.disconnect (); } catch (Exception e) {E.PrintStackTrace (); }finalmente{ } } }感谢阅读 , 希望能帮助到大家 谢谢大家对本站的支持! 谢谢大家对本站的支持!