Android Question Parsing BTC price from Binance API

Mattiaf

Active Member
Licensed User
Hi Im trying to create an alert for btc price using binance api, and in order to parse the price of btc i don't need to create a user key api.
I checked on the forum for similar topics and I found this one

and modified a bit the code to be used on b4x pages

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    
    Dim t As Timer
    t.Initialize("t",1000)
    t.Enabled=True
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True

End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.
Sub t_tick
    
    Dim j As HttpJob
    j.Download("https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT")
    Wait For (h) JobDone(h As HttpJob)
    Dim parser As JSONParser
    If j.Success Then
        
        parser.Initialize(j.GetString)
        Dim Root As Map = parser.NextObject
        Dim USDT As Double = Root.Get("price")
        Log("USDT= " & USDT) 
    End If
    j.Release
End Sub

but i'm getting few errors, eventho the op in the other topic said it worked..
B4X:
B4XMainPage - 40: Unknown member: get
B4XMainPage - 39: Current declaration does not match previous one.<br />Previous: {Type=B4XView,Rank=0, RemoteObject=True}<br />Current: {Type=Map,Rank=0, RemoteObject=True}
B4XMainPage - 13: This sub should only be used for variables declaration or assignments of primitive values. (warning #29)
B4XMainPage - 34: Undeclared variable 'h'. (warning #8)
B4XMainPage - 41: Undeclared variable 'usdt'. (warning #8)
B4XMainPage - 32: Variable 'j' was not initialized. (warning #11)

what is exactly the issue? thanks
 

mangojack

Well-Known Member
Licensed User
Longtime User
Correct Code ....
B4X:
'No Code to be placed in this Sub , Only Declarations !
Sub Class_Globals           
    Private Root As B4XView
    Private xui As XUI
    Dim t As Timer
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
 
    t.Initialize("t",1000)
    t.Enabled=True
End Sub

Sub t_tick
    GetData
End Sub

Sub GetData
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT")
    Wait For (j) JobDone(j As HttpJob)
    Dim parser As JSONParser
    If j.Success Then
    
        Log(j.GetString)   ' ####  Use this Link to generate JSON Parse Code  ###     http://basic4ppc.com:51042/json/index.html
    
        parser.Initialize(j.GetString)
        Dim jRoot As Map = parser.NextObject
        Dim USDT As String = jRoot.Get("price")
        Log("USDT= " & USDT)
    End If
    j.Release
End Sub

1. The most evident error was you were placing code in the Class Globals Sub ... Not Good.
2. Also you are using B4XPages which uses the variable "Root" , that you re-declared in the Timer sub

I have moved code out of the Timer Sub , into another Sub "GetData" .... Its just a personal thing , you can do it either way
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
Hi Im trying to create an alert for btc price using binance api, and in order to parse the price of btc i don't need to create a user key api.
I checked on the forum for similar topics and I found this one

and modified a bit the code to be used on b4x pages

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
  
    Dim t As Timer
    t.Initialize("t",1000)
    t.Enabled=True
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True

End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
  
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.
Sub t_tick
  
    Dim j As HttpJob
    j.Download("https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT")
    Wait For (h) JobDone(h As HttpJob)
    Dim parser As JSONParser
    If j.Success Then
      
        parser.Initialize(j.GetString)
        Dim Root As Map = parser.NextObject
        Dim USDT As Double = Root.Get("price")
        Log("USDT= " & USDT)
    End If
    j.Release
End Sub

but i'm getting few errors, eventho the op in the other topic said it worked..
B4X:
B4XMainPage - 40: Unknown member: get
B4XMainPage - 39: Current declaration does not match previous one.<br />Previous: {Type=B4XView,Rank=0, RemoteObject=True}<br />Current: {Type=Map,Rank=0, RemoteObject=True}
B4XMainPage - 13: This sub should only be used for variables declaration or assignments of primitive values. (warning #29)
B4XMainPage - 34: Undeclared variable 'h'. (warning #8)
B4XMainPage - 41: Undeclared variable 'usdt'. (warning #8)
B4XMainPage - 32: Variable 'j' was not initialized. (warning #11)

what is exactly the issue? thanks
The issue is mix of use with h and j as httpjob.
B4X:
Dim j As HttpJob
    j.Download("https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT")
    Wait For (j) JobDone(h As HttpJob)
    Dim parser As JSONParser
    If j.Success Then
        
        parser.Initialize(h.GetString)
        Dim Root As Map = parser.NextObject
        Dim USDT As Double = Root.Get("price")
        Log("USDT= " & USDT)
    End If
    j.Release
 
Upvote 0
Top