Page 1 of 1

Importing XML fails..randomly!

Posted: Fri Sep 12, 2014 10:42 am
by Weaksafety
Hi everyone,

this one is really making my head spin. There goes:

I've written a small app that queries an API and gets a xml with local weather, which then parses and structures into a card.

Everything was working fine initially, except that after a while I started getting "xmlerr: can't parse xml" on random occasions.
Retrying the same query sometimes resolves the issue, in other occasions it doesn't.

Now, I've attached the full stack if you wanted to try it out, but here's the relevant code that builds and launches the call to the API.

There's a function to build the API call ("costruisciChiamataAPI"), which then feeds the construction of the XML parsing.

Code: Select all

local citta, forecastGiornoArray, meteoOraArray

on mouseup
   get URL costruisciChiamataAPI() 
   put it into risultato
   put risultato into field "risultato"
   pubblicaXML(ottieniNodoXML(risultato))
end mouseup

function costruisciChiamataAPI  
   local apiKey="&key=878a3350da6dda93ce4a0d3b30a8122e8242796e"
   local apiURLhead="http://api.worldweatheronline.com/free/v1/weather.ashx?"
   local apiURLfoot="&format=xml&num_of_days=5&lang=it"
   get the text of field cittaInput
   if it is empty then 
      answer "Errore, campo vuoto"
      exit to top
   end if
   put it into citta
   put "q=" & URLEncode(citta) into cittaTarget  
   put apiURLhead & cittaTarget & apiURLfoot & apiKey into chiamataAPI
   return chiamataAPI
end costruisciChiamataAPI

function ottieniNodoXML albero  
   put revCreateXMLTree (albero, false, true, false) into tTree
   if tTree is not an integer then
      answer error "Problema con l'XML ricevuto, con nodo: " & tTree
      return empty
   end if
   return tTree
end ottieniNodoXML

on pubblicaXML tTree 
   controllaCittaValida(tTree)
   estraiDatiMeteoOra(tTree)
   estraiDatiMeteoForecast(tTree)
   revDeleteXMLTree tTree //cancella albero per liberare memoria
   lock screen  //alcuni elementi grafici non si aggiornano in tempo reale...
   visualizzaForecast
   visualizzaMeteoOra
   unlock screen
end pubblicaXML
The rest of the code is not relevant as the XML import does not go through after this. Again, it seems completely random.
Could it be an issue of the remote server? Or could it be that I'm not doing something properly and Livecode runs out of memory? (Sorry, I'm really clueless!)

Thanks a gazillion!

Mike

Re: Importing XML fails..randomly!

Posted: Fri Sep 12, 2014 1:48 pm
by Klaus
Hi Mike,

some hints:
1. NEVER use IT unless IT is mandatory like after displaying a dialog or whatever!
IT will change when you least exspect it:
...
## get URL costruisciChiamataAPI()
## put it into risultato
put URL costruisciChiamataAPI() into risultato
put risultato into field "risultato"
...
## get the text of field cittaInput
## QUOTES!
put the tex tof fld "cittaInput" into citta
if it is empty then
...

2. You do NOT CHECK for any ERRORS when retrieving stuff from the internet!? :shock:
Always check "the result" which should be empty on success or contains a hint on what might have gone wrong.


Best

Klaus

Re: Importing XML fails..randomly!

Posted: Fri Sep 12, 2014 2:21 pm
by Simon
Hi Mike,
What Klaus said.
But I found something odd, chiamataAPI had a space before the http.

Code: Select all

   put it into citta
   put "q=" & URLEncode(citta) into cittaTarget  //URLEncode serve per gestire in formato web gli spazi, caratteri speciali etc.
   put apiURLhead & cittaTarget & apiURLfoot & apiKey into chiamataAPI
   replace space with "" in chiamataAPI-- remove that space
   return chiamataAPI
And for me as well the space randomly showed up before the http.

Simon

Re: Importing XML fails..randomly!

Posted: Sat Sep 13, 2014 4:30 pm
by Weaksafety
Thanks a lot to both of you!

Ok, lessons learned. Yes, I'm definitely new at this so I didn't check for errors.. ooops :roll: it'll go at place #1 of my list.
The random error might be provoked by the space, then. I guess it'll just take a couple of edits and I should be good to go.

Many thanks again, I've still got a lot of ground to cover in programming! :D