欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-11 23:16:55
java调用天气webservice的小应用 废话不多说,直接贴代码:  cityreq.java package com.weather;...

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{
      
    }
  }
  
   
}

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