0

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Arshad Ali
  • 3,082
  • 12
  • 56
  • 99

1 Answers1

0

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");
        }
    }
Raja Jaganathan
  • 33,099
  • 4
  • 30
  • 33
  • Dear it does not work first it says Object type can not be converted to XML on { var errorXML:XML = result_weather_data; ) and says that a term has no properties or something like that – Arshad Ali Sep 26 '12 at 10:59
  • Basically if your service got result correct means your type is Object otherwise you got result type is XML so first need to check type like i mentioned above :) – Raja Jaganathan Sep 27 '12 at 06:22
  • dear i did it in your way it worked, but if event.result is XML then it says {Error #1010: A term is undefined and has no properties.} on line of {cityNameData.text = result_weather_data.data.request.query;} how can I solve that again . .. – Arshad Ali Sep 27 '12 at 11:09
  • Error #1010: means you asking property not available in that object before we access any property in object you need check via (in,hasOwnProperty()) after success you can play with it.If property not available problem with SERVER response result. You have to check with server code. If you want to see all properties in that object you can just use ObjectUtil.toString(result_weather_data) it will print all properties. – Raja Jaganathan Sep 27 '12 at 11:52