1

I am a beginner with livecode and i can't for the life of me figure out how to call for the API and use the data that it gives me. I have integrated the easyjson script in my stack and pasted the

" $_GET https://api.darksky.net/forecast/secretkeyhere/37.8267,-122.4233 "

and i get nothing. I am probably missing alot but i don't know how to get it to work, i've googled alot and to be honest there isn't that much about livecode stuff...

My goal is to create a weather app

Jolleboi
  • 11
  • 2

1 Answers1

0

$_GET is a LiveCode server-only construct. If you want to get data back from a web service API in a LiveCode stack, use a standard put statement, with the URL keyword:

put URL "https://api.darksky.net/forecast/secretkeyhere/37.8267,-122.4233" into tWeatherData

This is how you execute a GET method with a RESTful API. To use a POST method, use the LC post command:

# first construct the argument string
put "name=" & urlEncode("value string here") into tArgs
post tArgs to URL "http://api.base.url"
put it into tSomeVariable

I put together a lesson on how to access RESTful APIs in LiveCode here:

http://livecode.byu.edu/internet/webServicesIntro.php

Edit

Once you have downloaded the data you simply parse out what you want to display and show it in a text field. In the case of darksky.net, the data is sent as JSON text, so you can use LiveCode's JSON library to convert it to an array.

put JSONimport(tWeatherData) into tWeatherArray
put tWeatherArray["currently"]["temperature"] && "degrees and" \
  && tForecastArray ["currently"]["icon"] into field "currentWeather"
Devin
  • 593
  • 1
  • 3
  • 8
  • I've managed to access the API now and i get the degrees and if it's cloudy clear etc, but how do i make it show automatically on the card? Right now the data just comes in a pop up window if i press a button. I did like this: on mouseUp get url "https://api.darksky.net/forecast/secreykeyhere/59.334591,18.063240" put it into tForecast put JSONimport(tForecast) into tForecastArray answer "It is currently"&&tforecastarray[currently][temperature]&&"degrees and"&&tforecastarray[currently][icon] end mouseUp – Jolleboi Mar 13 '17 at 11:23
  • Using an answer command will show the results in a modal dialog. Instead, just create a field to display the weather data. Let's say, field "currentWeather". Replace the `answer` statement with `put tForecastArray["currently"]["temperature"] && "degrees and" && tForecastArray ["currently"]["icon"] into field "currentWeather" `. – Devin Mar 14 '17 at 14:30