Android Question Retrieve a string from webview

pazzokli

Active Member
Licensed User
Longtime User
Hi members,
I load in a webview this url "http://192.168.1.20/rpc/Shelly.GetStatus" in order to retrieve the status of my shelly device.
How can I use the answer showed in webview? Is possible convert it in a string?

B4X:
{"ble":{},"bthome":{},"cloud":{"connected":true},"input:0":{"id":0,"state":true},"knx":{},"mqtt":{"connected":false},"switch:0":{"id":0,"source":"switch","output":true,"temperature":{"tC":59.3,"tF":138.7}},"sys":{"mac":"34B7DA8E0BB0","restart_required":false,"time":"18:42","unixtime":1733679729,"uptime":612785,"ram_size":259896,"ram_free":92992,"fs_size":1048576,"fs_free":593920,"cfg_rev":16,"kvs_rev":0,"schedule_rev":0,"webhook_rev":0,"available_updates":{},"reset_reason":1},"wifi":{"sta_ip":"192.168.1.20","status":"got ip","ssid":"Home&Life","rssi":-49},"ws":{"connected":false}}

Thenks
 

drgottjr

Expert
Licensed User
Longtime User
B4X:
    Dim status As String = $"{"ble":{},"bthome":{},"cloud":{"connected":True},"input:0":{"id":0,"state":True},"knx":{},"mqtt":{"connected":False},"switch:0":{"id":0,"source":"switch","output":True,"temperature":{"tC":59.3,"tF":138.7}},"sys":{"mac":"34B7DA8E0BB0","restart_required":False,"time":"18:42","unixtime":1733679729,"uptime":612785,"ram_size":259896,"ram_free":92992,"fs_size":1048576,"fs_free":593920,"cfg_rev":16,"kvs_rev":0,"schedule_rev":0,"webhook_rev":0,"available_updates":{},"reset_reason":1},"wifi":{"sta_ip":"192.168.1.20","status":"got ip","ssid":"Home&Life","rssi":-49},"ws":{"connected":False}}"$
    Dim json As JSONParser
    json.Initialize(status)
    Dim walker As Map = json.NextObject
    Dim ble As Map = walker.Get("ble")
    Dim bthome As Map = walker.Get("bthome")
    Dim cloud As Map = walker.Get("cloud")
    Log("connected: " & cloud.Get("connected"))
    Dim input0 As Map = walker.Get("input:0")
    Log("id: " & input0.Get("id") & " state: " & input0.Get("state"))
    Dim kns As Map = walker.Get("knx")
    Dim mqtt As Map = walker.Get("mqtt")
    Log("mqtt connected: " & mqtt.Get("connected"))
    ' etc
    ' etc
    Dim wifi As Map = walker.get("wifi")
    Log("wifi:" & wifi.Get("sta_ip") & " status: " & wifi.Get("status") & " ssid: " & wifi.Get("ssid") & " rssi: " & wifi.Get("rssi"))

get the idea?
 

Attachments

  • cap.png
    34.1 KB · Views: 30
Upvote 0

pazzokli

Active Member
Licensed User
Longtime User
Friend,
I want try your code but I don't know how to convert text into webview to string

EDIT: I think I understood.... correct me if wrong
I need to avoid webview but use a code like this:


json:
Dim j As HttpJob
j.Initialize("", Me)
j.Download("https://www.google.com")
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
   Log(j.GetString)
End If
j.Release
 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
that's the way: j.download("http://192.168.1.20/rpc/Shelly.GetStatus")
then assign j.getstring to a variable and initialize the json parser with it:
json.initialize(j.getstring)
etc
etc
you don't have to parse the whole string (uness you want all the elements). if all you want is wifi status, just parse it:
Dim wifi As Map = walker.get("wifi")
Log("wifi:" & wifi.Get("sta_ip") & " status: " & wifi.Get("status") & " ssid: " & wifi.Get("ssid") & " rssi: " & wifi.Get("rssi"))
as in my example before. you do have to download the entire string; you just parse out what you want
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…