Android Question extract data from json and update label text in widget

Hello,

I would be great-full and happy if someone could help me out over here. I have searched a lot in the forums and tried multiple pieces of code but unfortunately I have issues.

I want to make a widget. I get the data from a web server in Json format. I am able to get it successfully and also parse it. My json contains multiple columns and rows.

Something like this,
B4X:
{
    "locations": [
        {
            "name": "My Home",
            "address": {
                "housenumber": "3",
                "country": "abc",
            },
            "rooms": [
                {
                    "name": "room1",
                    "appliances": [
                        {
                            "name": "device1",
                            "config": {
                                "thresholds": [
                                    {
                                        "quantity": "temperature",
                                        "type": "min"
                                    }
                                ]
                            },
                          
                            "data_latest": {
                                "measurement": {
                                    "temperature": 15.5,
                                    "humidity": 72,
                                    "battery": 60,
                                }
                            }
                        }
                    ],
                },
              
               {
                    "name": "room2",
                    "appliances": [
                        {
                            "name": "device2",
                            "config": {
                                "thresholds": [
                                    {
                                        "quantity": "temperature",
                                        "type": "min"
                                    },
                                ]
                            },
                          
                            "data_latest": {
                                "measurement": {
                                    "temperature": 15.5,
                                    "humidity": 72,
                                    "battery": 60,
                                }
                            }
                        }
                    ]
                },
                {
                    "name": "room3",
                    "appliances": [
                        {
                            "name": "device3",
                            "config": {
                                "thresholds": [
                                    {
                                        "quantity": "temperature",
                                        "type": "min"
                                    },
                                ]
                            },
                          
                            "data_latest": {
                                "measurement": {
                                    "temperature": 15.5,
                                    "humidity": 72,
                                    "battery": 60,
                                }
                            }
                        }
                    ]
                },
                {
                    "name": "room4",
                    "appliances": [
                        {
                            "name": "device4",
                            "config": {
                                "thresholds": [
                                    {
                                        "quantity": "temperature",
                                        "type": "min"
                                    },
                                ]
                            },
                          
                            "data_latest": {
                                "measurement": {
                                    "temperature": 15.5,
                                    "humidity": 72,
                                    "battery": 60,
                                }
                            }
                        }
                    ]
                }
            ]
        }
    ]
}


I also used the online json parcer tool. I can successfully extract the data.

B4A code,

B4X:
Sub JobDone (Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        Select Job.JobName
            Case "Job"
                'print the result to the logs
                Log("OpenBot" & Job.GetString)
            Case "Dashboard"
                Log("Dashboard" & Job.GetString)
                Dim parser As JSONParser
                parser.Initialize(Job.GetString)
                Dim root As Map = parser.NextObject
                      
              
             locations = root.Get("locations")
          
                For Each collocations As Map In locations
                    address = collocations.Get("address")
                    housenumber = address.Get("housenumber")
                    House_Name = collocations.Get("name")
                    Log(House_Name)
                  
      
                  
                rooms = collocations.Get("rooms")
              
  
                      
                For Each colrooms As Map In rooms
  
                        Room_Name = colrooms.Get("name")
                        appliances = colrooms.Get("appliances")
                      
                    For Each colappliances As Map In appliances
                        Device_Name = colappliances.Get("name")
                        data_latest = colappliances.Get("data_latest")
                        measurement = data_latest.Get("measurement")
                        temperature = measurement.Get("temperature")
                        humidity = measurement.Get("humidity")
                          
                            Log(Room_Name)
                            Log(Device_Name)
                            Log(temperature)
                            Log(humidity)
                          
                          
                          
                    Next
                  
                        Next
                Next

      
              
        End Select
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub


The thing I want to do is display "Room name", "Device_Name","temperature" and "humidity" for all 4 rooms on a widget.

How can I do this ?

I tried but what I get is only the last values. The logs works correctly. How can I update the text in the lables ?

Please help
 
Last edited:
The parsing code looks correct. Do you see the values of all 4 rooms in the logs?

Where is the code that updates the labels?


Log output,

B4X:
MY House
Room1
Device1
12
65
Room2
Device2
15
69
Room3
Device3
22
90
Room4
Device4
11
60


I am getting the correct output from the logs.

After calling JobDone I call Update_Data function to update the lables,

B4X:
Sub Update_Data
   
    rv.SetText("label1",House_Name)

    rv.UpdateWidget

End Sub


This is the designer view. I want to show the following text in the following labels,


Label1= House Name
Label2 = Room1
Label3 = Device1
Label4 = Temperature (value)
laber5 = Humidity (Value)

Similar for rest labels and panel
 

Attachments

  • designer_view.JPG
    designer_view.JPG
    29.6 KB · Views: 360
Upvote 0
Top