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

Discussion in 'Android Questions' started by Rushil Dahisaria, Jan 1, 2018.

Tags:
  1. Rushil Dahisaria

    Rushil Dahisaria New Member

    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,
    Code:
    {
        
    "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,

    Code:
    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: Jan 1, 2018
  2. Erel

    Erel Administrator Staff Member Licensed User

    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?
     
  3. Rushil Dahisaria

    Rushil Dahisaria New Member


    Log output,

    Code:
    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,

    Code:
    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
     

    Attached Files:

  4. Erel

    Erel Administrator Staff Member Licensed User

    Set the labels inside this loop:
    Code:
    For Each colappliances As Map In appliances
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice