Java 调用天气Webservice详解及实例代码

作者:lqh 时间:2021-10-09 21:59:03 

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 class CityReq {

private String theCityName;

public String getTheCityName() {
   return theCityName;
 }

@XmlElement(name="theCityName",namespace="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;
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.xml.soap.SOAPMessage;

import org.w3c.dom.Document;
public class WeatherWebServiceTest {

public static void main(String[] args) {
   // TODO Auto-generated method 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;
   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");

//发送数据
     ous=urlconn.getOutputStream();

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);
     //创建soapmessage对象
     SOAPMessage soapMessage=MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();
     SOAPBody soapBody=soapMessage.getSOAPBody();
     soapBody.addDocument(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(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(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 uresp= reponse.getValue();
     System.out.println(uresp.getResult());*/

ous.close();
     ins.close();
     urlconn.disconnect();
   } catch (Exception e) {
     e.printStackTrace();
   }finally{

}
 }

}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

标签:Java,天气Webservice
0
投稿

猜你喜欢

  • javaweb Servlet开发总结(二)

    2023-10-31 11:51:48
  • Java servlet、filter、listener、interceptor之间的区别和联系

    2023-11-02 15:32:17
  • Spring实战之SpEl语法实例详解

    2023-09-18 07:56:03
  • IntelliJ IDEA 下载安装超详细教程(推荐)

    2023-11-19 23:50:16
  • SpringBoot通过自定义注解实现参数校验

    2023-09-21 21:11:02
  • IDEA连接Mysql数据库的详细图文教程

    2023-10-09 09:51:24
  • Android用过TextView实现跑马灯效果的示例

    2023-07-27 11:16:54
  • Java构造代码块,静态代码块原理与用法实例分析

    2023-11-03 09:03:45
  • SpringBoot统一返回JSON格式实现方法详解

    2021-10-03 20:22:52
  • MyBatis 如何配置多个别名 typeAliasesPackage

    2021-11-16 06:35:54
  • 用Java实现简单ATM机功能

    2023-05-10 13:36:03
  • Spring Boot+Drools规则引擎整合详解

    2023-11-28 20:33:00
  • springboot远程debug调试全过程

    2023-11-25 07:05:56
  • Spring Bean生命周期之BeanDefinition的合并过程详解

    2023-11-29 02:50:35
  • ShardingSphere jdbc实现分库分表核心概念详解

    2023-11-24 12:09:45
  • Spring Cloud Gateway整合sentinel 实现流控熔断的问题

    2022-01-18 23:10:05
  • SpringBoot整合Web开发之Json数据返回的实现

    2023-04-27 05:06:51
  • Spring JDBCTemplate原理及使用实例

    2023-03-11 09:47:19
  • Java内部类知识汇总

    2023-08-18 14:06:54
  • Java自定义线程池的实现示例

    2022-01-23 01:28:04
  • asp之家 软件编程 m.aspxhome.com