Importing XML fails..randomly!

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
Weaksafety
Posts: 17
Joined: Mon Aug 12, 2013 5:20 pm

Importing XML fails..randomly!

Post by Weaksafety » Fri Sep 12, 2014 10:42 am

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
Attachments
WeathApp5.zip
the full stack
(192.92 KiB) Downloaded 60 times

Klaus
Posts: 12202
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Importing XML fails..randomly!

Post by Klaus » Fri Sep 12, 2014 1:48 pm

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

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Importing XML fails..randomly!

Post by Simon » Fri Sep 12, 2014 2:21 pm

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Weaksafety
Posts: 17
Joined: Mon Aug 12, 2013 5:20 pm

Re: Importing XML fails..randomly!

Post by Weaksafety » Sat Sep 13, 2014 4:30 pm

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

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”