Android Tutorial Using fixer.io to get current Currency rates

This Tutorial is how to use the free service from fixer.io
in your own apps.

Fixer.io is a free JSON API for current and historical foreign exchange rates published by the European Central Bank.

The rates are updated daily around 3PM CET.

Dependencies: okHTTPutils2, JSON


Retrieve most current rates (updated daily from european central bank)


B4X:
    Dim job As HttpJob
    job.Initialize("FixerIOList",Me)
    job.Download("http://api.fixer.io/latest")

B4X:
Sub JobDone (Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        Select Job.JobName

            Case "FixerIOList"
                'print the result to the logs
                Log(Job.GetString)
                ToastMessageShow(Job.GetString,True)
                Dim parser As JSONParser
                parser.Initialize(Job.GetString)
                Dim root As Map = parser.NextObject
                Dim date As String = root.Get("date")
                Dim rates As Map = root.Get("rates")
                'Dim CHF As Double = rates.Get("CHF")
                'Dim HRK As Double = rates.Get("HRK")
                'Dim MXN As Double = rates.Get("MXN")
                'Dim ZAR As Double = rates.Get("ZAR")
                'Dim INR As Double = rates.Get("INR")
                'Dim CNY As Double = rates.Get("CNY")
                'Dim THB As Double = rates.Get("THB")
                'Dim AUD As Double = rates.Get("AUD")
                'Dim ILS As Double = rates.Get("ILS")
                'Dim KRW As Double = rates.Get("KRW")
                'Dim JPY As Double = rates.Get("JPY")
                'Dim PLN As Double = rates.Get("PLN")
                'Dim GBP As Double = rates.Get("GBP")
                'Dim IDR As Double = rates.Get("IDR")
                'Dim HUF As Double = rates.Get("HUF")
                'Dim PHP As Double = rates.Get("PHP")
                'Dim Try As Double = rates.Get("TRY")
                'Dim RUB As Double = rates.Get("RUB")
                'Dim HKD As Double = rates.Get("HKD")
                'Dim DKK As Double = rates.Get("DKK")
                'Dim CAD As Double = rates.Get("CAD")
                'Dim MYR As Double = rates.Get("MYR")
                'Dim USD As Double = rates.Get("USD")
                'Dim BGN As Double = rates.Get("BGN")
                'Dim NOK As Double = rates.Get("NOK")
                'Dim RON As Double = rates.Get("RON")
                'Dim SGD As Double = rates.Get("SGD")
                'Dim CZK As Double = rates.Get("CZK")
                'Dim SEK As Double = rates.Get("SEK")
                'Dim NZD As Double = rates.Get("NZD")
                'Dim BRL As Double = rates.Get("BRL")
                'Dim base As String = root.Get("base")
            
        End Select
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release

End Sub

Retrieve rates from a specific day since 1999

B4X:
    Dim job As HttpJob
    job.Initialize("GetHistoricalRate",Me)
    job.Tag = "Rates of 2000-01-03"
    job.Download("http://api.fixer.io/2000-01-03")

B4X:
Sub JobDone (Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        Select Job.JobName
            Case "GetHistoricalRate"
                'print the result to the logs
                Log(Job.Tag)

                Log(Job.GetString)
                ToastMessageShow(Job.GetString,True)
                Dim parser As JSONParser
                parser.Initialize(Job.GetString)
                Dim root As Map = parser.NextObject
                Dim date As String = root.Get("date")
                Dim rates As Map = root.Get("rates")
                Log(rates)

            
        End Select
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release

End Sub

Retrieve rates based on EUR and return USD and GBP

B4X:
    Dim job As HttpJob
    job.Initialize("FixerConvert",Me)
    job.Download("http://api.fixer.io/latest?base=EUR&symbols=USD,GBP")

B4X:
Sub JobDone (Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        Select Job.JobName
            Case "FixerConvert"
                'print the result to the logs
                Log(Job.Tag)

                Log(Job.GetString)
                ToastMessageShow(Job.GetString,True)
                Dim parser As JSONParser
                parser.Initialize(Job.GetString)
                Dim root As Map = parser.NextObject
                Dim date As String = root.Get("date")
                Dim rates As Map = root.Get("rates")
                Log(rates)
            
        End Select
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release

End Sub
 
Last edited:

Mahares

Expert
Licensed User
Longtime User
Since @DonManfred wrote it before Resumable Subs, I simply adapted his code using Resumable Sub:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    DownloadRates("http://api.fixer.io/latest")
End Sub
Sub DownloadRates(Link As String)
    Dim job As HttpJob
    job.Initialize("", Me)
    job.Download(Link)
    Wait For (job) JobDone(job As HttpJob)
    If job.Success Then
        Log(job.GetString)
        ToastMessageShow(job.GetString,True)
        Dim parser As JSONParser
        parser.Initialize(job.GetString)
        Dim root As Map = parser.NextObject
        Dim date As String = root.Get("date")
        Dim rates As Map = root.Get("rates")
        Dim USD As Double = rates.Get("USD")      
        Dim EUR As String =root.Get("base")   
        Log($"The ${EUR} is worth ${USD}  US Dollar on ${date}"$)   
      
    Else
        Log("Error: " & job.ErrorMessage)
        ToastMessageShow("Error: " & job.ErrorMessage, True)
    End If
    job.Release
End Sub
 
Last edited:

mangojack

Expert
Licensed User
Longtime User
Last edited:
Top