Java 调用天气 WebService 的小应用
废话不多说, 直接贴代码:
CityReq.java
แพ็คเกจ com.weather; นำเข้า javax.xml.bind.annotation.xmlelement; นำเข้า Javax.xml.bind.annotation.xmlrootelement; @xmlrootelement (name = "getweathercityname", namespace = "http://webxml.com สตริงสาธารณะ gettheCityName () {return thecityName; } @xmleLelement (name = "thecityName", namespace = "http://webxml.com.cn/") โมฆะสาธารณะ settheCityName (String theCityName) {this.theCityName = theCityName; -WeatherwebserviceTest.java
แพ็คเกจ com.weather; นำเข้า java.io.inputstream; นำเข้า java.io.OutputStream; นำเข้า Java.net.httpurlConnection; นำเข้า java.net.url; นำเข้า Javax.xml.bind.jaxbcontext; javax.xml.parsers.documentbuilderfactory; นำเข้า Javax.xml.soap.messagefactory; นำเข้า javax.xml.soap.soapbody; นำเข้า Javax.xml.soap.soapconstants; org.w3c.dom.document; Public Class WeatherwebserviceTest {โมฆะคงที่สาธารณะหลัก (String [] args) {// todo วิธีการที่สร้างขึ้นอัตโนมัติสภาพอากาศ (); } สภาพอากาศโมฆะคงที่ () {System.out.println ("开始登陆 ... "); สตริง 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 ใหม่ (wsdl); urlConn = (httpurlConnection) U.OpenConnection (); urlConn.SetDoOutput (จริง); urlconn.setRequestMethod ("โพสต์"); 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, Printwriter ใหม่ (System.out)); Marsh.Marshal (XMLF, เอกสาร); // 创建 SOAPMESSAGE 对象 SOAPMESSAGE SOAPMESSAGE = MessageFactory.NewInstance (SOAPCONSTANTES.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 respmsg = new StringBuffer (); ไบต์ [] ไบต์ = ไบต์ใหม่ [1024*1024]; int a = -1; ในขณะที่ ((a = ins.read (ไบต์))! =-1) {respmsg.append (สตริงใหม่ (ไบต์, 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 <ITERSPLES> Reponse = unmarsh.unmarshal (Responsemessage.getSoapbody (). ExtractContentAstoCument (), CityResp.Class); CityResp uresp = reponse.getValue (); System.out.println (uresp.getResult ());*/ ous.close (); ins.close (); urlconn.disconnect (); } catch (exception e) {e.printstacktrace (); }ในที่สุด{ } } }感谢阅读, 希望能帮助到大家, 谢谢大家对本站的支持!