最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java 調(diào)用天氣Webservice詳解及實(shí)例代碼

 更新時(shí)間:2016年11月26日 14:28:08   投稿:lqh  
這篇文章主要介紹了Java 調(diào)用天氣Webservice詳解及實(shí)例代碼的相關(guān)資料,這里附實(shí)例代碼,使用java 調(diào)用webservice 的小應(yīng)用,需要的朋友可以參考下

Java調(diào)用天氣Webservice的小應(yīng)用

廢話不多說,直接貼代碼:

 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");
      
      //發(fā)送數(shù)據(jù)
      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);
      //創(chuàng)建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");
      //發(fā)送數(shù)據(jù)
      soapMessage.writeTo(ous);
      // soapMessage.writeTo(System.out);
      System.out.println(urlconn.getResponseCode());
      System.out.println(urlconn.getResponseMessage());
      //接收數(shù)據(jù)
      ins=urlconn.getInputStream();
      //接收的數(shù)據(jù)需要解組?
      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{
      
    }
  }
  
   
}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • JAVA實(shí)現(xiàn)經(jīng)典掃雷游戲的示例代碼

    JAVA實(shí)現(xiàn)經(jīng)典掃雷游戲的示例代碼

    windows自帶的游戲《掃雷》是陪伴了無數(shù)人的經(jīng)典游戲,本程序參考《掃雷》的規(guī)則進(jìn)行了簡化,用java語言實(shí)現(xiàn),采用了swing技術(shù)進(jìn)行了界面化處理。感興趣的可以學(xué)習(xí)一下
    2022-01-01
  • Java動態(tài)顯示當(dāng)前日期和時(shí)間

    Java動態(tài)顯示當(dāng)前日期和時(shí)間

    這篇文章主要為大家詳細(xì)介紹了Java動態(tài)顯示當(dāng)前日期和時(shí)間,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • 解決idea啟動報(bào)錯javax.imageio.IIOException的問題

    解決idea啟動報(bào)錯javax.imageio.IIOException的問題

    這篇文章主要介紹了idea啟動報(bào)錯javax.imageio.IIOException,解決打不開idea問題,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • SpringBoot Test及注解的使用詳解

    SpringBoot Test及注解的使用詳解

    這篇文章主要介紹了SpringBoot Test及注解的使用,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • SpringMVC如何在生產(chǎn)環(huán)境禁用Swagger的方法

    SpringMVC如何在生產(chǎn)環(huán)境禁用Swagger的方法

    本篇文章主要介紹了SpringMVC如何在生產(chǎn)環(huán)境禁用Swagger的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • java字符串拼接與性能分析詳解

    java字符串拼接與性能分析詳解

    在JAVA中拼接兩個字符串的最簡便的方式就是使用操作符”+”。如果你用”+”來連接固定長度的字符串,可能性能上會稍受影響,但是如果你是在循環(huán)中來”+”多個串的話,性能將指數(shù)倍的下降,下面我們分析一下JAVA字符串拼接的性能
    2014-01-01
  • mybatis調(diào)用存儲過程,帶in、out參數(shù)問題

    mybatis調(diào)用存儲過程,帶in、out參數(shù)問題

    這篇文章主要介紹了mybatis調(diào)用存儲過程,帶in、out參數(shù)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • 淺談MyBatis-plus入門使用

    淺談MyBatis-plus入門使用

    這幾天本人了解到了MyBatis-plus,一個 Mybatis 增強(qiáng)工具包.經(jīng)過一番研究,發(fā)現(xiàn)這玩意真的好用,不用寫任何 xml ,內(nèi)置通用的 Mapper,而且完全是面向?qū)ο缶幊?文檔給的示例代碼,跟之前用過的 sequelize (Node.js 的 ORM)非常像,因此本人也嘗試了一把, 需要的朋友可以參考下
    2021-05-05
  • 基于SpringBoot實(shí)現(xiàn)定時(shí)發(fā)送郵件過程解析

    基于SpringBoot實(shí)現(xiàn)定時(shí)發(fā)送郵件過程解析

    這篇文章主要介紹了基于SpringBoot實(shí)現(xiàn)定時(shí)發(fā)送郵件過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • 關(guān)于SpringCloud的Bus消息總線圖文詳解

    關(guān)于SpringCloud的Bus消息總線圖文詳解

    這篇文章主要介紹了關(guān)于SpringCloud的Bus消息總線圖文詳解,Spring Cloud Bus是用來將分布式系統(tǒng)的節(jié)點(diǎn)與輕量級消息系統(tǒng)鏈接起來的框架,它整合了Java的事件處理機(jī)制和消息中間件的功能,需要的朋友可以參考下
    2023-05-05

最新評論

崇信县| 逊克县| 崇州市| 五家渠市| 长治市| 龙州县| 湖州市| 东至县| 赫章县| 化隆| 乐昌市| 马公市| 营山县| 连山| 五常市| 定日县| 安丘市| 商洛市| 朝阳县| 大港区| 通州市| 衢州市| 广灵县| 潼关县| 台南市| 渝中区| 泊头市| 西安市| 嘉荫县| 于都县| 阳高县| 阿克苏市| 宁津县| 海门市| 房产| 宁河县| 绥化市| 昭平县| 林芝县| 宜川县| 佛教|