Java调用天气ウェブサービス的小应用
废话不多说、直接贴代码:
cityreq.java
パッケージcom.weather; import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmlrootelement; @xmlrootelement; @xmlrootelement(name = "getweatherbycityname"、namespace = "http://webxml.citname public string getTheCityName(){return thecityName; } @xmlelement(name = "thecityname"、namepace = "http://webxml.com.cn/")public void setthecityName(string thecityName){this.thecityName = thecityName; }}Weatherwebservicetest.java
パッケージ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.parsers.documentbuilderfactory; Import javax.xml.soap.messageFactory; Import javax.xml.soap.soapbody; import javax.xml.soap.soapconstants; Import javax.xml.soap.soapenvelope; Import javax.xxml.soap.soap.soapenage; org.w3c.dom.document; public class heatherwebservicetest {public static void main(string [] args){// todo auto-enerated method stub weather(); } static void Weather(){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; try {url u = new url(wsdl); urlconn =(httpurlconnection)u.openconnection(); urlconn.setDoOutput(true); urlconn.setRequestMethod( "post"); urlconn.setRequestProperty( "content-type"、 "application/soap+xml; charset = utf-8"); //urlconn.setRequestProperty("content-type "、" text/xml; charset = utf-8 "); // document document = 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); // oapmessage Soapbody soapbody = soapmessage.getsoapbody(); soapbody.addddocument(document); 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(system.out); System.out.println(urlconn.getResponseCode()); System.out.println(urlconn.getResponsemessage()); // //接收的数据需要解组? stringbuffer respmsg = new StringBuffer(); byte [] bytes = new byte [1024*1024]; int a = -1; while((a = ins.read(bytes))!= -1){respmsg.append(new String(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()。extractcontentasdocument()、cityresp.class); cityResp uesp = reponse.getValue(); system.out.println(uresp.getResult());*/ ous.close(); ins.close(); urlconn.disconnect(); } catch(Exception e){e.printstacktrace(); }ついに{ } } }感谢阅读、希望能帮助到大家、谢谢大家对本站的支持!