Android Question How to download string from a website as a JSON

Erind Jushaj

Member
Licensed User
Hi everyone,

I have spent a few days looking online but I could not find anywhere a solution thats why I am asking here.

There is a certain (type) of website which I would like to load on my app and download the info from it. For example: https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USDT&e=Binance will give a result such as {"USDT":3803.67}. I would like to download this info so that later I can use the values from it. I have done something similat in VBA Excel where I download it as a JSON and modify it further.

Can someone please help?

Many thanks
 

Mahares

Expert
Licensed User
Longtime User
Based on the 2 links @DonManfred provided you with in post #2, and since it is your first post in the forum, here is the complete code you need in the project:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    DownloadString    'IMPORTANT: need Json and OkHttpUtils2 libraries
End Sub

Sub DownloadString
    Dim j As HttpJob
    j.Initialize("", Me) 
    j.Download("https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USDT&e=Binance")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim parser As JSONParser
        parser.Initialize(j.GetString)
        Dim root As Map = parser.NextObject
        Dim USDT As Double = root.Get("USDT")       
        Log("USDT= " & USDT)  'displays: USDT= 3823.19  .The value changes slightly every time you run
    End If
    j.Release
End Sub
 
Upvote 0

Erind Jushaj

Member
Licensed User
Guys, I cannot thank you enough. I have spent weeks programming it into Excel VBA and dreamt to do something similar for Android and thanks to you I might be able to do so. Thanks again!!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…