Android Example Parse JSON Weather Files from openweathermap.org

First go to https://openweathermap.org/price and get APIKEY
Insert in the APIKEY in source code where are (INSERT_YOUR_APID_HERE).
Read the web for alternatives forecast options.


Create a new project and copy insert this code. Select the necesary libs. JSON-HTTP-HTTPUTILS2

Read and Debug the code. This is a 5 day forecast-

B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: JSON Weather Example
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: unspecified
#End Region

'Activity module
Sub Process_Globals
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)

' USAR LIBRERIAS
' HTTP + HTTPUTILS2 +JSON
Dim job As HttpJob

job.Initialize("WE",Me)
' goto  http://api.openweathermap.org
' and get APPID , see instructions and options

job.Download("http://api.openweathermap.org/data/2.5/forecast?lat=-36.77&lon=-64.85&units=metric&appid=INSERT_YOUR_APID_HERE")

End Sub

Sub JobDone (Job As HttpJob)

   If Job.Success = True Then
           parserjson(Job.GetString)
    End If
End Sub
   
Sub parserjson(jsontext As String)
   
' THE BEGINING ----------------------------
Dim parser As JSONParser
parser.Initialize(jsontext)
Dim Maestro As Map = parser.NextObject

                                            Log(jsontext)
'TIP:  Debug and stop In next line.
' Select Maestro and Analize
' See size (num items)
' Items like String and Numbers
' Items with +
' Touch +city and look like MAP
' Touch +list  and look like AN ARRAR of MAPS

'  -----PARSE MAESTRO- 3 items- 1 map - 1 Array--------------------------
Dim cnt As Int = Maestro.Get("cnt")
Dim cod As String = Maestro.Get("cod")
Dim message As Double = Maestro.Get("message")
Dim MapaCiudad As Map = Maestro.Get("city")   ' MAP
Dim DailyReport As List = Maestro.Get("list")        'ARRAY

'  -----PARSE MAPACIUDAD - 3 items- 2 map --------------------------
Dim Pais As String = MapaCiudad.Get("country")
Dim name As String = MapaCiudad.Get("name")
Dim id As Int = MapaCiudad.Get("id")
Dim sys As Map = MapaCiudad.Get("sys")          ' MAP
    Dim population As Int = sys.Get("population")
                                                                    Log("population")
Dim LatLonMap As Map = MapaCiudad.Get("coord")   'MAP
    Dim lon As Double = LatLonMap.Get("lon")
    Dim Lat As Double = LatLonMap.Get("lat")

                                                                    Log(name&"-"&Pais&"-"&Lat&"-"&lon)
'  -----PARSE DailyReport From MAESTRO--------------------------
    For Each Report As Map In DailyReport       
        Dim dt As Int = Report.Get("dt")
        Dim dt_txt As String = Report.Get("dt_txt")
                                                                    Log(dt&"-----"&dt_txt)
        Dim xmain As Map = Report.Get("main")               
            Dim temp As Double = xmain.Get("temp")
            Dim temp_min As Double = xmain.Get("temp_min")
            Dim grnd_level As Double = xmain.Get("grnd_level")
            Dim temp_kf As Int = xmain.Get("temp_kf")
            Dim humidity As Int = xmain.Get("humidity")
            Dim pressure As Double = xmain.Get("pressure")
            Dim sea_level As Double = xmain.Get("sea_level")
            Dim temp_max As Double = xmain.Get("temp_max")

        Dim clouds As Map = Report.Get("clouds")
            Dim all As Int = clouds.Get("all")

        Dim sys As Map = Report.Get("sys")
            Dim pod As String = sys.Get("pod")
       
        Dim wind As Map = Report.Get("wind")
            Dim deg As Int = wind.Get("deg")
            Dim speed As Double = wind.Get("speed")
                                                                    Log("WIND"&"="&speed&"="&deg)   
' rain and snow sometimes no download
' Make error, if read null data 
' Get rain or snow and check isinitliazed       
' Check Item and read
        Dim rain As Map = Report.Get("rain") 
        If rain.IsInitialized Then
            If rain.ContainsKey("3h") Then 
                Dim x3h As Double = rain.Get("3h")
                Log(x3h)
            Else
                Dim x3h As Double =0.0
            End If
        End If
        Dim snow As Map = Report.Get("snow")
            If snow.IsInitialized Then
            If snow.ContainsKey("3h") Then 
                Dim xx3h As Double = snow.Get("3h")
                Log(xx3h)
            Else
                Dim xx3h As Double =0.0
            End If
        End If
        Dim weather As List = Report.Get("weather")  
        For Each colweather As Map In weather
            Dim icon As String = colweather.Get("icon")
            Dim description As String = colweather.Get("description")
            Dim smain As String = colweather.Get("main")
            Dim id As Int = colweather.Get("id")
        Next

    Next

End Sub


Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Is not a class of JSON or HTTP. In the forum are a lot of example.
Only work for me and share.
 
Top