Hi. I'm trying to create a class that includes a pair of subs to request json data. When I try it inside the Main it works fine. But when transferring the events to the class, the program does not wait for the answer and the complete execution of the program ends. This is the code:
Since it did not work, I deleted the sub jobDone and wrote this new code:
But it stays the same. When i run the program step by step, When you run the program step by step, the program upon reaching the 'wait for' statement the program is broken.
The entire class, for now is:
Example of use of class
¿Because the main works, and class, no?
¿I wonder if the response can be received asynchronously, so that the program continues, and when the data arrives, run the jobDone, as it was initially planned?
B4X:
private Sub SendCommand(work As String, uri As String)
Try
Dim nonce As Long
nonce = DateTime.now
uri = uri & "&nonce=" & nonce
Dim sign() As Byte
sign = HashHmac(uri, API_Secret)
Dim API_Signed As String
Dim Byte_Conv As ByteConverter
API_Signed = Byte_Conv.HexFromBytes(sign) 'convert to HEX
API_Signed = API_Signed.ToLowerCase
Dim j As HttpJob
j.Initialize(work, "money")
j.PostString(uri, "")
j.GetRequest.SetHeader("apisign", API_Signed)
Catch
Log(LastException)
End Try
End Sub
private Sub jobDone (job As HttpJob)
Try
Log(job.Success)
Log(job.GetString)
If (job.JobName == "CmdGetTicker") Then
Dim parser As JSONParser
parser.Initialize(job.GetString)
Dim root As Map = parser.NextObject
Dim result As Map = root.Get("result")
Dim Last As Double = result.Get("Last")
LastOld = LastNow
LastNow = Last
Dim Ask As Double = result.Get("Ask")
AskOld = AskNow
AskNow = Ask
Dim Bid As Double = result.Get("Bid")
BidOld = BidNow
BidNow = Bid
Dim success As String = root.Get("success")
status = success 'Save status
Dim message As String = root.Get("message")
End If
'guardamos la hora de al recepcion de los datos
TimeOld = DateTime.Now
Catch
Log(LastException)
End Try
End Sub
Since it did not work, I deleted the sub jobDone and wrote this new code:
B4X:
private Sub SendCommand(work As String, uri As String)
Try
Dim nonce As Long
nonce = DateTime.now
uri = uri & "&nonce=" & nonce
Dim sign() As Byte
sign = HashHmac(uri, API_Secret)
Dim API_Signed As String
Dim Byte_Conv As ByteConverter
API_Signed = Byte_Conv.HexFromBytes(sign) 'convert to HEX
API_Signed = API_Signed.ToLowerCase
Dim j As HttpJob
j.Initialize(work, "money")
j.PostString(uri, "")
j.GetRequest.SetHeader("apisign", API_Signed)
Wait For (j) jobDone(j As HttpJob)
If j.Success Then
If (j.JobName == "CmdGetTicker") Then
Dim parser As JSONParser
parser.Initialize(j.GetString)
Dim root As Map = parser.NextObject
Dim result As Map = root.Get("result")
Dim Last As Double = result.Get("Last")
LastOld = LastNow
LastNow = Last
Dim Ask As Double = result.Get("Ask")
AskOld = AskNow
AskNow = Ask
Dim Bid As Double = result.Get("Bid")
BidOld = BidNow
BidNow = Bid
Dim success As String = root.Get("success")
status = success 'Save Status
Dim message As String = root.Get("message")
End If
End If
Catch
Log(LastException)
End Try
End Sub
But it stays the same. When i run the program step by step, When you run the program step by step, the program upon reaching the 'wait for' statement the program is broken.
The entire class, for now is:
B4X:
Sub Class_Globals
Dim name As String
Private API_Key As String
Private API_Secret As String
Dim BidOld As Double
Dim AskOld As Double
Dim LastOld As Double
Dim TimeOld As Long
Dim BidNow As Double
Dim AskNow As Double
Dim LastNow As Double
Dim status As String
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(money As String, Key As String, Secret As String)
name = money
API_Key = Key
API_Secret = Secret
End Sub
private Sub HashHmac(data As String, secret As String) As Byte()
Try
Dim m As Mac 'm As Message Authentication Code
Dim kg As KeyGenerator 'kg As KeyGenerator
kg.Initialize("HmacSHA512") 'initialize kg using HmacSHA512 algorithm
kg.KeyFromBytes(secret.GetBytes("UTF8")) 'encode string "secret" to an array of Bytes using UTF8
m.Initialise("HmacSHA512", kg.Key) 'initialize m using HmacSHA512 algorithm and the secret key
m.Update(data.GetBytes("UTF8")) 'encodes post data to an array of Bytes and loads it to be signed
Return m.Sign 'sign the loaded data using the secret key, return the calc signature data
Catch
Log(LastException)
End Try
End Sub
private Sub SendCommand(work As String, uri As String)
Try
Dim nonce As Long
nonce = DateTime.now
uri = uri & "&nonce=" & nonce
Dim sign() As Byte
sign = HashHmac(uri, API_Secret)
Dim API_Signed As String
Dim Byte_Conv As ByteConverter
API_Signed = Byte_Conv.HexFromBytes(sign) 'convert to HEX
API_Signed = API_Signed.ToLowerCase
Dim j As HttpJob
j.Initialize(work, "money")
j.PostString(uri, "")
j.GetRequest.SetHeader("apisign", API_Signed)
Wait For (j) jobDone(j As HttpJob)
If j.Success Then
If (j.JobName == "CmdGetTicker") Then
Dim parser As JSONParser
parser.Initialize(j.GetString)
Dim root As Map = parser.NextObject
Dim result As Map = root.Get("result")
Dim Last As Double = result.Get("Last")
LastOld = LastNow
LastNow = Last
Dim Ask As Double = result.Get("Ask")
AskOld = AskNow
AskNow = Ask
Dim Bid As Double = result.Get("Bid")
BidOld = BidNow
BidNow = Bid
Dim success As String = root.Get("success")
status = success 'Guardamos el estatus
Dim message As String = root.Get("message")
End If
End If
Catch
Log(LastException)
End Try
End Sub
private Sub jobDone (job As HttpJob)
Try
Log(job.Success)
Log(job.GetString)
If (job.JobName == "CmdGetTicker") Then
Dim parser As JSONParser
parser.Initialize(job.GetString)
Dim root As Map = parser.NextObject
Dim result As Map = root.Get("result")
Dim Last As Double = result.Get("Last")
LastOld = LastNow
LastNow = Last
Dim Ask As Double = result.Get("Ask")
AskOld = AskNow
AskNow = Ask
Dim Bid As Double = result.Get("Bid")
BidOld = BidNow
BidNow = Bid
Dim success As String = root.Get("success")
status = success 'Set estatus
Dim message As String = root.Get("message")
End If
'Save Time
TimeOld = DateTime.Now
Catch
Log(LastException)
End Try
End Sub
public Sub Update
Try
If (TimeOld + 1000 < DateTime.Now) Then
Dim uri As String
uri = "https://bittrex.com/api/v1.1/public/getticker?apikey=" & API_Key & "&market=BTC-ETH"
SendCommand("CmdGetTicker", uri)
End If
Catch
Log(LastException)
End Try
End Sub
Example of use of class
B4X:
Dim ETH As money
API_Key = "fc10b55fd5014141a6ab3b5b6ce3745c"
API_Secret = "d499182d9a2542b48d0d248bc5289baa"
ETH.Initialize("Ethereum", API_Key, API_Secret)
ETH.Update
¿Because the main works, and class, no?
¿I wonder if the response can be received asynchronously, so that the program continues, and when the data arrives, run the jobDone, as it was initially planned?