Focused on this approach now, I have found a free and open weather service at timeanddate.com, to illustrate this web-access model. The app downloads a file containing a pre-formatted list of cities which is displayed in a ListView(), and the corresponding webpage for a selected city will then be streamed through the ReceiveHTTPMemory() function. The weather information will then be extracted and displayed.
Code: Select all
EnableExplicit
CompilerIf #PB_Compiler_Version < 600
InitNetwork()
CompilerEndIf
Global Dim cities.s(149)
Define event, appQuit, cityList, cityLabel, forecastLabel, urlLabel
Procedure initData()
Shared cityList
Define i, citiesFile.s
citiesFile = GetTemporaryDirectory() + "cities.txt"
If FileSize(citiesFile) < 1
ReceiveHTTPFile("syed.sg/tutorials/cities.txt", citiesFile)
EndIf
If ReadFile(0, citiesFile)
While Eof(0) = 0
cities(i) = Trim(ReadString(0))
i + 1
Wend
CloseFile(0)
EndIf
For i = 0 To 149
AddGadgetItem (cityList, -1, cities(i))
Next i
EndProcedure
Procedure getWeather(city.s)
Shared cityList, cityLabel, forecastLabel, urlLabel
Define.s weatherURL, rawResponse, hiLow = "HTTP error!"
Define *buffer, responseSize.q, hiLowPos
SetGadgetText(cityLabel, city)
city = ReplaceString(ReplaceString(city, ", ", "/"), " ", "-")
weatherURL = "https://www.timeanddate.com/weather/" + LCase(city)
SetGadgetText(urlLabel, weatherURL)
*buffer = ReceiveHTTPMemory(weatherURL)
If *buffer
responseSize = MemorySize(*buffer)
rawResponse = PeekS(*buffer, responseSize, #PB_UTF8 | #PB_ByteLength)
FreeMemory(*buffer)
If Not FindString(rawResponse, "Unknown address", 0)
hiLowPos = FindString(rawResponse, "High and low forecasted temperature today", 0)
hiLow = Trim(Mid(rawResponse, hiLowPos + 53, 7), "&") + " °C"
EndIf
EndIf
SetGadgetText(forecastLabel, hiLow)
EndProcedure
OpenWindow(0, 0, 0, 900, 500, "ReceiveHTTPMemory() Example - Weather App",
#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
cityList = ListViewGadget(#PB_Any, 10, 10, 350, 420)
cityLabel = TextGadget(#PB_Any, 400, 30, 480, 100, "Select a city...")
forecastLabel = TextGadget(#PB_Any, 450, 135, 430, 300, "High / Low")
urlLabel = TextGadget(#PB_Any, 10, 460, 880, 30,
"https://timeanddate.com/weather", #PB_Text_Center)
If LoadFont(0, "Arial", 30)
SetGadgetFont(cityLabel, FontID(0))
EndIf
If LoadFont(1, "Arial", 70)
SetGadgetFont(forecastLabel, FontID(1))
EndIf
initData()
Repeat
event = WaitWindowEvent()
Select event
Case #PB_Event_CloseWindow
appQuit = #True
Case #PB_Event_Gadget
Select EventGadget()
Case cityList
If EventType() = #PB_EventType_LeftClick
getWeather(GetGadgetText(cityList))
EndIf
EndSelect
EndSelect
Until appQuit
