Unity3d Request WebService

Source: Internet
Author: User
Tags assert soap wsdl

When we are docking the third-party SDK, the third-party SDK is usually used as a webservice interface for us to invoke. These interfaces are accessed by providing protocols such as our get,post,soap. The Get,post method is believed to be familiar to everyone, and today we focus on the SOAP protocol access.

Soap, also known as a Simple Object access protocol, is a protocol specification for exchanging data, and SOAP is XML-based. WebService the three elements contain SOAP, WSDL, one of the UDDI, soap describes the format of the passing of information, WSDL is used to describe how to access the specific interface, UDDI for management, distribution, query WebService. SOAP can be used in conjunction with many existing Internet protocols and formats, including Hypertext Transfer Protocol (HTTP), Simple Mail Transfer Protocol (SMTP), Multipurpose Internet Mail Expansion Protocol (MIME). It also supports a large number of applications, from the messaging system to Remote procedure calls (RPCs). SOAP uses a combination of XML-based data structures and Hypertext Transfer Protocol (HTTP) to define a standard method for using distributed objects in various operating environments on the Internet. For more information, you can refer to the relevant information online, soap and WSDL tutorials can be seen soap,wsdl.

Let's make a request for a weather forecast example. The WebService interface for free weather testing is available online, but is limited to the number of usage times per day. More interface information can be accessed by http://www.webxml.com.cn/zh_cn/index.aspx. See http://www.webxml.com.cn/zh_cn/index.aspx find the "GetWeather" interface we need for our weather forecast, and we can see that this interface provides access to Post,get and soap. Our applet uses post and SOAP access methods, so we'll look at how he provides post and soap usage.

  

This is how the post is used, and we're going to start with a parameter request with Thecitycode=string&Theuserid= string to WebService. Which thecitycode for the city name such as "Shenzhen", or Shenzhen corresponding code "2419", can not be empty. Theuserid is a member ID, can be empty, is free to use. The following is a request return, which is an array of characters in XML form.

  

This is how SOAP access is used, and we construct an XML soap with soap access, where Thecitycode and Theuserid are the same arguments as the previous post method. Well, it's our turn to play Unity3d.

We first use unity to draw the effect we need, because we want to do a weather forecast of the small program, we can first draw a blue sky background, in doing a few dynamic clouds, to achieve the beautification effect. Effect

Create a new script and bind it to the canvas our clouds are rolling up.

YunScroll.cs

usingUnityengine;usingSystem.Collections; Public classyunscroll:monobehaviour{Private Const intBg_w =937; Private Const floatMspeed =30.0f;    Transform yun1;    Transform yun2; //Use this for initialization    voidStart () {yun1= transform. Findchild ("yun1"); Debug.Assert (yun1!=NULL,"The object cannot be found"); Yun2= transform. Findchild ("yun2"); Debug.Assert (yun2!=NULL,"The object cannot be found"); }    //Update is called once per frame    voidUpdate () {//Yun1. Translate (Vector3.left * time.deltatime *mspeed); if(Yun1.position.x <=-(bg_w/2) ) {yun1.position=NewVector3 (bg_w + (bg_w/2), YUN1.POSITION.Y, yun1.position.z); }        //Yun2. Translate (Vector3.left * time.deltatime *mspeed); if(Yun2.position.x <=-(bg_w/2) ) {yun2.position=NewVector3 (bg_w + (bg_w/2), YUN2.POSITION.Y, yun2.position.z); }    }}

Well, our sky cloud background has moved, and we add the content and picture space to show the weather. Effect

Add the script Weatherscript and bind to the main camera.

WeatherScript.cs:

usingUnityengine;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem;usingSystem.Text;usingSystem.Xml;usingUnityengine.ui; Public classweatherscript:monobehaviour{enumRequest_type {POST, SOAP,} PublicText title;  PublicText Todayinfo;  Publicgameobject[] panels; Privatedictionary<int, transform[]> contextdic =Newdictionary<int, transform[]>(); //Use this for initialization    voidStart () { for(inti =0; I < panels. Length; ++i) {transform[] Objs=Newtransform[4]; objs[0] = Panels[i].transform. Findchild (" Day"); objs[1] = Panels[i].transform. Findchild ("Temperature"); objs[2] = Panels[i].transform. Findchild (" Wind"); objs[3] = Panels[i].transform. Findchild ("Image"); Contextdic[i]=Objs; }//textasset textasset = (textasset) resources.load ("weather_2");//Parsingxml (Textasset.text, request_type.soap);    }    //Update is called once per frame    voidUpdate () {}/// <summary>    ///Post Call/// </summary>     Public voidOnpost () {Startcoroutine (Posthandler ()); }    /// <summary>    ///SOAP Calls/// </summary>     Public voidOnsoap () {Startcoroutine (Soaphandler ()); } IEnumerator Posthandler () {wwwform form=NewWwwform (); Form. AddField ("Thecitycode","Shenzhen"); Form. AddField ("Theuserid",""); WWW W=NewWWW ("Http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather", form); yield returnW; if(w.isdone) {if(W.error! =NULL) {print (w.error); }            Else{parsingxml (W.text, request_type.post); }}} IEnumerator Soaphandler () {StringBuilder soap=NewStringBuilder (); Soap. Append ("<?xml version=\ "1.0\" encoding=\ "utf-8\"?>"); Soap. Append ("<soap12:envelope xmlns:xsi=\ "http://www.w3.org/2001/xmlschema-instance\" xmlns:xsd=\ "http://www.w3.org/ 2001/xmlschema\ "xmlns:soap12=\" http://www.w3.org/2003/05/soap-envelope\ ">"); Soap. Append ("<soap12:Body>"); Soap. Append ("<getweather xmlns=\ "http://webxml.com.cn/\" >"); Soap. Append ("<theCityCode> Shenzhen </theCityCode>"); Soap. Append ("<theUserID></theUserID>"); Soap. Append ("</getWeather>"); Soap. Append ("</soap12:Body>"); Soap. Append ("</soap12:Envelope>"); Wwwform form=NewWwwform (); varheaders =form.headers; headers["Content-type"] ="text/xml; Charset=utf-8"; headers["SOAPAction"] ="Http://WebXml.com.cn/getWeather"; headers["user-agent"] ="gsoap/2.8"; WWW W=NewWWW ("Http://ws.webxml.com.cn/WebServices/WeatherWS.asmx", Encoding.UTF8.GetBytes (soap.        ToString ()), headers); yield returnW; if(w.isdone) {if(W.error! =NULL) {print (w.error); }            Else{parsingxml (W.text, Request_type.soap); }        }    }    Private voidParsingxml (string_xml,request_type _type) {XmlDocument xmldoc=NewXmlDocument ();        Xmldoc.loadxml (_xml); XmlNode Arrofstr=xmldoc.documentelement; XmlNodeList Childnode=NULL; #regionPOSTif(_type = =request_type.post) {Childnode=Arrofstr.childnodes; }        #endregion        #regionSoapElse if(_type = =Request_type.soap)            {Xmldoc.loadxml (arrofstr.innerxml); Arrofstr=xmldoc.documentelement;            Xmldoc.loadxml (Arrofstr.innerxml); Arrofstr=xmldoc.documentelement;            Xmldoc.loadxml (Arrofstr.innerxml); Arrofstr=xmldoc.documentelement; Childnode=Arrofstr.childnodes; }        #endregiontitle. Getcomponent<Text> (). Text = String.Format ("<color=red>{0}</color>. {1},{2}", childnode[0]. INNERXML, childnode[4]. INNERXML, childnode[5].        INNERXML); Todayinfo.getcomponent<Text> (). Text = childnode[6].        INNERXML;  for(inti =0; i < Contextdic.count; ++i) {contextdic[i][0]. Getcomponent<text> (). Text = childnode[7+ I *5].            INNERXML; contextdic[i][1]. Getcomponent<text> (). Text = childnode[8+ I *5].            INNERXML; contextdic[i][2]. Getcomponent<text> (). Text = childnode[9+ I *5].            INNERXML; stringstr =string. Format ("a_{0}", childnode[Ten+ I *5]. Innerxml.split ('.')[0]); Sprite SP= Resources.load (str,typeof(Sprite)) asSprite; if(sp! =NULL) {contextdic[i][3]. Getcomponent<image> (). Sprite =sp; }        }    }}

Bind the object to the script corresponding to the variable, add an event response to the button, OK, we have a simple weather forecast application to get out, we look at the effect of it.

Reprint Please specify source: http://www.cnblogs.com/fyluyg/p/6047819.html

Download

Unity3d Request WebService

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.