Getting information from a web server

I want to get data from a web server and find ch_id in it and display it in a TextBox or Label.
This is what I did


But I don't get anything in Label1, please help.

You need to change several things:

  • Use a Web component instead of a WebViewer
  • Get from that URL in the Web component
  • in the Web.Got Text event, grab the text into a global variable to help debug
  • Use the Web.JSONDecodeAsDictionaries block to extract a dictionary from the JSON, then save it into another global variable init as empty dictionary.
  • The data you want is in the dictionary under this path: "data",1, "CH_id"

Thanks for the answer
I didn't find the Web.JSONDecodeAsDictionaries block and took JsonTextDecode but in the textbox I get "html"

I tried to get JSON data from https://raw.githubusercontent.com/cheprasov/json-colors/master/colors.json and everything works.

I understand that this is an authorization problem on the home web server.

When I used WebViewer, it opened http://192.168.0.157:5050/Login.srf?username=user&password=12345 with the first request and authorized, and then opened http://192.168.0.157:5050/Card.srf?c_no=212-60519 and received JSON data.

How can I make authorization in the Web component and receive JSON data?

I apologize for not remembering the exact block name. Here's the block for a good JSON decode:

I revisited the WebView doc page at
https://ai2-appinventor-mit-edu.ezproxy.canberra.edu.au/reference/components/userinterface.html#WebViewer

in hope of finding a real easy and simple way to grab text returned from a WebView.
(I haven't been there in a long while.)
Apparently, the web page you are visiting needs to be rigged with some Javascript to set a special WebViewString variable that can be grabbed by the WebView component after the WebView event that tells you it came back with a response.

I notice you are using a local IP address, and maybe you might be up to changing your html to return the JSON in that WebViewString, if you have a strong stomach for JavaScript.

Alternatively, here is an experiment for you to try.

The WebView component has an event that tells you when the WebView got a response:
component_event

Maybe if you do a single WebView call to pass authorization, I am hoping you might be able to issue a Web (not WebView) Get in that event, and the Web component would be able to come back with the JSON in its responseContent in its Web1.GotText event?

P.S. Other users on this board undoubtably have dealt with this issue before.

P.P.S. Show your Designer, so we can distinguish between textbox and webview in your sample outputs.

1 Like
1 Like

Hello
Thank you very much for your help ABG and TIMAI2.
I got the data from WebView to Label1

{ "success": true, "total": 1, "data": [ { "log_id": 307527, "log_msg_id": 1, "code_value": 26, "c_no": "212-60519", "c_id": 665, "ch_id": 804, "ch_type": 0, "ag_id": 10007, "ag_highlight": 0, "c_dg_privilege": "rw", "ch_dg_privilege": "rw", "log_controller_id": 333, "log_door_id": 0, "log_utc_value": "2024/08/19 11:45:31", "c_type": "Normal", "c_status": "Active", "c_activation": "", "c_deactivation": "", "ch_name": "Carter Tom", "log_msg": "Access Granted", "access_date": "2024-08-19" } ] }

But how do I get the ch_id values ​​in Label2 (for example: ch_id=804)


Please help me again.
Best regards, carborn

I did it like this, but it is not correct, because if the text length changes I will get other data. As I understand WebView String Changevalue I get text and not JSON Data and I need to get value from ch_id key, but how to do it?

Best regards, carborn

See points 4 and 5.

You still need to decode the JSON text into a dictionary and navigate through the dictionary.

The delivery method changed, not the decode.

See

the first link for the docs on dictionaries, and the block to get one from the JSON.