There are multiple methods for Flex to call Webservice. You can use the tools provided by Flex Builder to manage Webservices to parse the WSDL document and generate a local call class. You can also use the Webservice class in Action Script. The <Webservice> component is used for implementation. This time I used the last one. I feel that this method is more concise.
1. Build the interface. It is used to accept user input and result output. Includes: one Text input component, one button component, and 10 label components.
2. Add the Webservice component. Construct the Webservice component according to the instructions of the Web service provider. The Code is as follows:
<Mx: WebService id = "ws" wsdl = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx? Wsdl"
UseProxy = "false" showBusyCursor = "true">
<Mx: operation name = "getWeatherbyCityName" result = "resultOK ()">
<Mx: request>
<TheCityName>
{City. text. toString ()}
</TheCityName>
</Mx: request>
</Mx: operation>
</Mx: WebService>
Note: The name attribute in the operation component must be the same as the name of the method to be used. The property of result is the Action Script method executed after the method is called successfully. Operation also has other attributes. In view of the simple examples, operation will not be used one by one.
3. Compile the Action Script method. In this example, three methods are used: resultOK () is used to display the result, showMessage () is used to display the description, and getfocus () is used to clear the content of the input column when the input column gets the focus. The following is the analysis of resultOK:
Public function resultOK (): void {// display the accepted result
ArrayResult = new ArrayCollection ();
ArrayResult = ws. getWeatherbyCityName. lastResult;
// Display the current situation
Jin1.text = arrayResult [6]. toString (). substring (0, arrayResult [6]. toString (). indexOf (""));
Jin2.text = arrayResult [6]. toString (). substring (arrayResult [6]. toString (). indexOf ("") + 1 );
Jin3.text = arrayResult [5];
Jin4.text = arrayResult [7];
// Display tomorrow's situation
Ming1.text = arrayResult [13]. toString (). substring (0, arrayResult [13]. toString (). indexOf (""));
Ming2.text = arrayResult [13]. toString (). substring (arrayResult [13]. toString (). indexOf ("") + 1 );
Ming3.text = arrayResult [12];
Ming4.text = arrayResult [14];
// Display the day after tomorrow
Hou1.text = arrayResult [18]. toString (). substring (0, arrayResult [18]. toString (). indexOf (""));
Hou2.text = arrayResult [18]. toString (). substring (arrayResult [18]. toString (). indexOf ("") + 1 );
Hou3.text = arrayResult [17];
Hou4.text = arrayResult [19];
}
When using the results returned by the Web service, it is found that the results are saved in the character Array, which is ArrayOfString In the WSDL, but Flex does not have this format. After trying the String and Array types, the ArrayCollection class is available. The result is displayed. You can select the information according to the WSDL description.
4. Use the button to call the Webservice component and send Web service information. That is, the ws. getWeatherbyCityName. send () method is called.
Compile and publish the swf file.
When using swf files, I don't know how to embed the swf exported by flex into Html. Normal embedding does not allow you to call Web Services. You can use the Html file in the file package generated by the project. However, if you copy the file package and place it in another place, you cannot use the Web service. I am very grateful to the expert for help.
The complete code is as follows:
<? Xml version = "1.0" encoding = "gb2312"?>
<Mx: Application xmlns: mx = "http://www.adobe.com/2006/mxml" width = "330"
Height = "155" layout = "absolute" fontSize = "12" viewSourceURL = "srcview/index.html">
<Mx: Script>
<! [CDATA [
Import mx. controls. Alert;
Import mx. collections. ArrayCollection;
Public var arrayResult: ArrayCollection; // Save the received result
Public function resultOK (): void {// display the accepted result
ArrayResult = new ArrayCollection ();
ArrayResult = ws. getWeatherbyCityName. lastResult;
// Display the current situation
Jin1.text = arrayResult [6]. toString (). substring (0, arrayResult [6]. toString (). indexOf (""));
Jin2.text = arrayResult [6]. toString (). substring (arrayResult [6]. toString (). indexOf ("") + 1 );
Jin3.text = arrayResult [5];
Jin4.text = arrayResult [7];
// Display tomorrow's situation
Ming1.text = arrayResult [13]. toString (). substring (0, arrayResult [13]. toString (). indexOf (""));
Ming2.text = arrayResult [13]. toString (). substring (arrayResult [13]. toString (). indexOf ("") + 1 );
Ming3.text = arrayResult [12];
Ming4.text = arrayResult [14];
// Display the day after tomorrow
Hou1.text = arrayResult [18]. toString (). substring (0, arrayResult [18]. toString (). indexOf (""));
Hou2.text = arrayResult [18]. toString (). substring (arrayResult [18]. toString (). indexOf ("") + 1 );
Hou3.text = arrayResult [17];
Hou4.text = arrayResult [19];
}
// Display description
Public function showMessage (): void {
Var alert: Alert = Alert. show ("Enter the Chinese name of the city (English is available for foreign cities ). Web services from http://www.webxml.com.cn, data from the China Meteorological Administration "+
", Including over 340 China" +
"Weather conditions in major cities and more than 60 major foreign cities within three days. Author EMAIL: anhulife@gmail.com "," NOTE ", Alert. YES );
}
// Processing when the input column gets the focus, the input column is cleared.
Public function getfocus (): void {
City. text = "";
}
]>
</Mx: Script>
<Mx: WebService id = "ws" wsdl = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx? Wsdl"
UseProxy = "false" showBusyCursor = "true">
<Mx: operation name = "getWeatherbyCityName" result = "resultOK ()">
<Mx: request>
<TheCityName>
{City. text. toString ()}
</TheCityName>
</Mx: request>
</Mx: operation>
</Mx: WebService>
<Mx: TextInput id = "city" text = "enter the city name to support major domestic and foreign cities"
Width = "200" x = "10" y = "10" focusIn = "getfocus ()" fontSize = "12" color = "#909697"/>
<Mx: Button id = "check" x = "230" y = "10" label = "query" click = "ws. getWeatherbyCityName. send ()"/>
<! -- Display the Label of the result -->
<Mx: Label x = "10" y = "42" text = "" width = "90" id = "jin1"/>
<Mx: Label x = "10" y = "70" text = "" width = "90" id = "jin2"/>
<Mx: Label x = "10" y = "98" text = "" width = "90" id = "jin3"/>
<Mx: Label x = "10" y = "126" text = "" width = "90" id = "jin4"/>
<Mx: Label x = "120" y = "42" text = "" width = "90" id = "ming1"/>
<Mx: Label x = "120" y = "70" text = "" width = "90" id = "ming2"/>
<Mx: Label x = "120" y = "98" text = "" width = "90" id = "ming3"/>
<Mx: Label x = "120" y = "126" text = "" width = "90" id = "ming4"/>
<Mx: Label x = "230" y = "41" text = "" width = "90" id = "hou1"/>
<Mx: Label x = "230" y = "69" text = "" width = "90" id = "hou2"/>
<Mx: Label x = "230" y = "97" text = "" width = "90" id = "hou3"/>
<Mx: Label x = "230" y = "125" text = "" width = "90" id = "hou4"/>
<! -- Display the description Label -->
<Mx: label x = "288" y = "12" text = "Description" width = "32" textDecoration = "underline" color = "# FAFBFB" click = "showMessage () "/>
</Mx: Application>
Author: "liuwang8888"