我正在尝试从 free.worldweatheronline.com 的 xml Web 服务获取数据,如果在 Web 服务中找到城市,我会给出城市名称,它会返回如下数据:
<data><request><type>City</type><query>Hyderabad, india</query></request><current_condition><observation_time>06:04 AM</observation_time><temp_C>34</temp_C><temp_F>92</temp_F><weatherCode>113</weatherCode><weatherIconUrl>http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png</weatherIconUrl><weatherDesc>Sunny</weatherDesc><windspeedMiles>14</windspeedMiles><windspeedKmph>22</windspeedKmph><winddirDegree>230</winddirDegree><winddir16Point>SW</winddir16Point><precipMM>0.0</precipMM><humidity>50</humidity><visibility>10</visibility><pressure>1011</pressure><cloudcover>0</cloudcover></current_condition>
我的 http 服务弹性代码是这样的:
<s:HTTPService id="weatherService"
url="{BASE_URL}"
resultFormat="object"
result="weatherService_resultHandler(event)"
fault="weatherService_faultHandler(event)"
showBusyCursor="true">
<s:request xmlns="">
<q>{cityName.text.toString()}</q>
<format>{FORMAT}</format>
<num_of_days>{NUMBER_OF_DAYS}</num_of_days>
<key>{API_KEY}</key>
</s:request>
</s:HTTPService>
这是处理代码:
private static const BASE_URL:String="http://free.worldweatheronline.com/feed/weather.ashx";
private static const API_KEY:String="MY_API_KEY";
private static const NUMBER_OF_DAYS:uint=2;
private static const FORMAT:String="xml";
protected function weatherService_resultHandler(event:ResultEvent):void
{
// TODO Auto-generated method stub
var result_weather_data:Object = event.result;
cityNameData.text=result_weather_data.data.request.query;
}
protected function weatherService_faultHandler(event:FaultEvent):void
{
// TODO Auto-generated method stub
}
如果未找到该城市,则返回如下 xml 数据:
<data><error><msg>Unable to find any matching weather location to the query submitted!</msg></error></data>
我正在尝试这样做
var error_msg:String = result_weather_data.data.error.msg;
在上面代码中的 cityNameData.text 之前,但它给出了未定义属性或类似内容的错误