Question

I'm trying to get data from a xml web service of free.worldweatheronline.com first I give name of city if the city is found in web service it returns data some thing like this:

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

my flex code for http service is like this:

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

and this is handling code:

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          

}

and if the city is not found it returns xml data like this:

<data><error><msg>Unable to find any matching weather location to the query submitted!</msg></error></data>

i'm trying to do like this

var error_msg:String = result_weather_data.data.error.msg; 

before cityNameData.text from above code but it gives an error of that undefined property or something like that

Was it helpful?

Solution

If property not exists in xml it will throws error

//Also note root name no need to specify to access this children node

//You need to check that property availbe or not in XML like this

        if(event.result is XML)
    {           
        var errorXML:XML = event.result as XML; 

        if(errorXML && errorXML.hasOwnPerperty("error") )
        {
            if(errorXML.error && errorXML.error.hasOwnProperty("msg"))
            {
                var yourErrorMsg:String = errorXML.error.msg; 
            }
        }
    }           
    else if(event.result is Object)
    {
        var result_weather_data:Object = event.result;

        if(result_weather_data && result_weather_data.hasOwnProperty('data'))
        {
            if(result_weather_data.data && result_weather_data.data.hasOwnProperty("request"))
            {
                if(result_weather_data.data.request && result_weather_data.data.request.hasOwnProperty("query"))
                {
                    cityNameData.text = result_weather_data.data.request.query;
                }
                else
                {
                    trace("query property not exists in result_weather_data.data.request Object");
                }
            }
            else
            {
                trace("request property not exists in result_weather_data.data Object");
            }
        }
        else
        {
            trace("result_weather_data is NULL or data property not exists in result_weather_data Object");
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top