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

 更新时间:2016年11月26日 14:28:08   投稿:lqh  
这篇文章主要介绍了Java 调用天气Webservice详解及实例代码的相关资料,这里附实例代码,使用java 调用webservice 的小应用,需要的朋友可以参考下

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实现经典扫雷游戏的示例代码

    JAVA实现经典扫雷游戏的示例代码

    windows自带的游戏《扫雷》是陪伴了无数人的经典游戏,本程序参考《扫雷》的规则进行了简化,用java语言实现,采用了swing技术进行了界面化处理。感兴趣的可以学习一下
    2022-01-01
  • Java动态显示当前日期和时间

    Java动态显示当前日期和时间

    这篇文章主要为大家详细介绍了Java动态显示当前日期和时间,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-12-12
  • 解决idea启动报错javax.imageio.IIOException的问题

    解决idea启动报错javax.imageio.IIOException的问题

    这篇文章主要介绍了idea启动报错javax.imageio.IIOException,解决打不开idea问题,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • SpringBoot Test及注解的使用详解

    SpringBoot Test及注解的使用详解

    这篇文章主要介绍了SpringBoot Test及注解的使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • SpringMVC如何在生产环境禁用Swagger的方法

    SpringMVC如何在生产环境禁用Swagger的方法

    本篇文章主要介绍了SpringMVC如何在生产环境禁用Swagger的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • java字符串拼接与性能分析详解

    java字符串拼接与性能分析详解

    在JAVA中拼接两个字符串的最简便的方式就是使用操作符”+”。如果你用”+”来连接固定长度的字符串,可能性能上会稍受影响,但是如果你是在循环中来”+”多个串的话,性能将指数倍的下降,下面我们分析一下JAVA字符串拼接的性能
    2014-01-01
  • mybatis调用存储过程,带in、out参数问题

    mybatis调用存储过程,带in、out参数问题

    这篇文章主要介绍了mybatis调用存储过程,带in、out参数问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • 浅谈MyBatis-plus入门使用

    浅谈MyBatis-plus入门使用

    这几天本人了解到了MyBatis-plus,一个 Mybatis 增强工具包.经过一番研究,发现这玩意真的好用,不用写任何 xml ,内置通用的 Mapper,而且完全是面向对象编程,文档给的示例代码,跟之前用过的 sequelize (Node.js 的 ORM)非常像,因此本人也尝试了一把, 需要的朋友可以参考下
    2021-05-05
  • 基于SpringBoot实现定时发送邮件过程解析

    基于SpringBoot实现定时发送邮件过程解析

    这篇文章主要介绍了基于SpringBoot实现定时发送邮件过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • 关于SpringCloud的Bus消息总线图文详解

    关于SpringCloud的Bus消息总线图文详解

    这篇文章主要介绍了关于SpringCloud的Bus消息总线图文详解,Spring Cloud Bus是用来将分布式系统的节点与轻量级消息系统链接起来的框架,它整合了Java的事件处理机制和消息中间件的功能,需要的朋友可以参考下
    2023-05-05

最新评论