Java 调用天气Webservice详解及实例代码
Java调用天气Webservice的小应用
废话不多说,直接贴代码:
CityReq.java
packagecom.weather;
importjavax.xml.bind.annotation.XmlElement;
importjavax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="getWeatherbyCityName",namespace="http://WebXml.com.cn/")
publicclassCityReq{
privateStringtheCityName;
publicStringgetTheCityName(){
returntheCityName;
}
@XmlElement(name="theCityName",namespace="http://WebXml.com.cn/")
publicvoidsetTheCityName(StringtheCityName){
this.theCityName=theCityName;
}
}
WeatherWebServiceTest.java
packagecom.weather;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.net.HttpURLConnection;
importjava.net.URL;
importjavax.xml.bind.JAXBContext;
importjavax.xml.bind.Marshaller;
importjavax.xml.parsers.DocumentBuilderFactory;
importjavax.xml.soap.MessageFactory;
importjavax.xml.soap.SOAPBody;
importjavax.xml.soap.SOAPConstants;
importjavax.xml.soap.SOAPEnvelope;
importjavax.xml.soap.SOAPMessage;
importorg.w3c.dom.Document;
publicclassWeatherWebServiceTest{
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
weather();
}
staticvoidweather(){
System.out.println("开始登陆...");
Stringwsdl="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";
System.out.println("wsdl:"+wsdl);
HttpURLConnectionurlconn=null;
InputStreamins=null;
OutputStreamous=null;
try{
URLu=newURL(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");
//发送数据
ous=urlconn.getOutputStream();
Documentdocument=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
//编组
Marshallermarsh=JAXBContext.newInstance(CityReq.class).createMarshaller();
CityReqxmlf=newCityReq();
xmlf.setTheCityName("北京");
//JAXB.marshal(xmlf,newPrintWriter(System.out));
marsh.marshal(xmlf,document);
//创建soapmessage对象
SOAPMessagesoapMessage=MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();
SOAPBodysoapBody=soapMessage.getSOAPBody();
soapBody.addDocument(document);
SOAPEnvelopesoapEnvelope=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();
//接收的数据需要解组?
StringBufferrespMsg=newStringBuffer();
byte[]bytes=newbyte[1024*1024];
inta=-1;
while((a=ins.read(bytes))!=-1){
respMsg.append(newString(bytes,0,a));
}
System.out.println(respMsg.length());
System.out.println(respMsg);
//解组的方式
/*SOAPMessageresponseMessage=MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(null,ins);
Unmarshallerunmarsh=JAXBContext.newInstance(CityResp.class).createUnmarshaller();
JAXBElement<CityResp>reponse=unmarsh.unmarshal(responseMessage.getSOAPBody().extractContentAsDocument(),CityResp.class);
CityRespuresp=reponse.getValue();
System.out.println(uresp.getResult());*/
ous.close();
ins.close();
urlconn.disconnect();
}catch(Exceptione){
e.printStackTrace();
}finally{
}
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!