Android Question Yahoo currency conversion

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,

I am attempting to change the server used in Erels Currency Converter Example, from:
URL = "http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency="
to Yahoo currency converter.

I thought it would be easy. [Idiot]

I have found samples Java code for Yahoo currency conversion and tried lifting the URL. Fail.
I am Web address inept. Please help

Regards
Roger
 

Roycefer

Well-Known Member
Licensed User
Longtime User
People trying to help you will want to see what code you've tried, links to the Java snippets you found, and URLs attempted.
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
the last one should be really easy with httputils. Hsve you tried?
For the others i guess you should search for the documentation of this API.
I´m pretty sure it can be easily handled with httputils too
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
the yahoo one just returns an XML set where the <ASK> paremeter value if probably the rate.

when you look at the USDEUR parameters the ASK value is 0.9444.

so $1 = €0.9444
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Thanks Guys,

I am still stumbling around trying things. I am sure it is simple if I had the vaguest ides of what I am doing.
This is the code that works, commented out are various attempts to use Yahoo.

B4X:
Sub ConvToView_ItemClick (Position As Int, ResultsValue As Object)
    Private temp As String
    toCountry = "ABC"
    Select AllData
        Case 0: Disp = LengthData(Position,3):  temp = LengthData(Position,1)
        Case 1:    Disp = AreaData(Position,3):    temp = AreaData(Position,1)   
        Case 2: Disp = VolumeData(Position,3):  temp = VolumeData(Position,1)   
        Case 3: Disp = MassData(Position,3):    temp = MassData(Position,1)       
        Case 4: Disp = SpeedData(Position,3):   temp = SpeedData(Position,1)
        Case 5: Disp = PowerData(Position,3):   temp = PowerData(Position,1)
        Case 6: toCountry = countries.get(Position)
    End Select
    toCountry = toCountry.SubString2(0, 3)
   
    If AllData <> 6 Then            'Do for all except Currency
        Buffer = ""
        StackPush
        send_to_display(Disp)
        If Two_flag = 1 Then
            Idisplay.Text = IDispTXT
            Two_flag = 0
        End If
        Idisplay.Text = temp
    Else
        'http://download.finance.yahoo.com/d/quotes.csv?s=GBPEUR=X&f=l1
'**********************************************************************************************************
        Private request As HttpRequest
        URL = "http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency="
        'URL = "http://download.finance.yahoo.com/d/quotes.csv?s="       'GBPEUR=X&f=l1"
        'URL = "http://download.finance.yahoo.com/d/quotes.csv?s= GBPEUR=X&f=l1"
       
        request.InitializeGet(URL & fromCountry & "&ToCurrency=" & toCountry)
        'request.InitializeGet(URL & fromCountry & toCountry)
        'request.InitializeGet(URL)
       
       
        request.Timeout = 10000 'set timeout to 10 seconds
        If HttpClient1.Execute(request, 1) = False Then Return 'Will be false if their is already a running task (with the same id).
        ProgressDialogShow("Calling server...")                            'get Currency and output result
'***************************************************************************************************************   
    End If
   
    ConvToView.RemoveView                                    'Exit ListView.
    lblTitle.Top = 0%y
    ListBack.Top = lblTitle.Top
    lblTitle.SendToBack
    ListBack.SendToBack
    ListFlag = 0
    ConvToView.Initialize("ConvToView")
End Sub

Sub HttpClient1_ResponseSuccess (Response As HttpResponse, TaskId As Int)
    ProgressDialogHide
    Private result,i, i2,  FromDisp, ToDisp As String
    Private ToResult As Double
    result = Response.GetString("UTF8")                     'Convert the response to a string
   
    Log("Result = " &result)
   
    Private rate As Double
    'Parse the result
    i = result.IndexOf(".NET/")
    If i = -1 Then
        Msgbox("Invalid response.", "Error")
        Return
    End If
    i2 = result.IndexOf2("<", i + 1)
    rate = result.substring2(i + 7, i2)
    rate = Max(rate,0)
    If IsNumber(Rdisplay.Text) = False Then
        Msgbox("Please enter a valid number.", "Error")
        Return
    End If
    'Set the answer
    FromDisp = Rdisplay.Text&"   "&fromCountry
    ToResult = Round2(Rdisplay.Text*rate,2)
    ToDisp = ToResult&"   "&toCountry&"     "
    If rate = 0 Then ToDisp = "----   "&toCountry&"     "
    Rdisplay.Text = FromDisp
    Idisplay.Text = ToDisp
End Sub


Any suggestions welecome.

Regards Roger
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The currency converter is a very old example. You should always use HttpUtils2.

The web service doesn't seem to work properly. It always returns -1:
http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=USD&ToCurrency=AFA

You can use this code to access this web service:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim j As HttpJob
   j.Initialize("currency", Me)
   j.Download2("http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate", _
     Array As String("FromCurrency", "USD", "ToCurrency", "EUR"))
End Sub

Sub JobDone(job As HttpJob)
   If job.Success Then
     Log(job.GetString)
     Dim m As Matcher = Regex.Matcher(">([-\d\.]+)<", job.GetString)
     If m.Find Then
       Log($"Rate = $1.2{m.Group(1)}"$)
     Else
       Log("Error parsing result")
     End If
   Else
     Log("Error!!!")
   End If
   job.Release
End Sub
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Thanks Erel,

www.webservicex.net is certainly the problem. Unfortunately using HttpUtils2 doesn't change anything. [Rate = -1] I have had good reports of the Yahoo service and am trying use that. Unfortunately I have zero knowledge on how to do this, I don't have sufficient understanding to frame a sensible question.


Regards Roger
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I guess you better read some http & xml tutorials first because you need a combination of those for the Yahoo one.
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,

I have been reading a lot and understanding a little. I have a solution to the problem, probably not the best solution but it works.
The code below has been extracted from the project and as such you have to assume that I have declared/initialized/etc some variables.
This Link is useful and shows how to download other related data.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules
   
    Private countries As List
    Private fromCountry, toCountry, FromTo As String
    Private XRate As HttpJob
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Portrait")
    Canvas1.Initialize(Activity)
    XRate.Initialize("currency", Me)
End Sub

Sub CurrencyFrom                      
'Load list with countries
   If countries.IsInitialized = False Then countries.Initialize
   If countries.Size = 0 Then countries = File.ReadList(File.DirAssets, "CountryCodes.txt"): countries.Sort(True)
        '"CountryCodes.txt" is a list from Erel's Currency Converter example.
End Sub


Sub ConvFromView_ItemClick (Position As Int, Unit As Object)
    If Not(F_common) Then Return
    Private temp As String = 1
    fromCountry = "ABC"
    Select AllData
            Case 0: temp = LengthData(Position,2)
            Case 1:    temp = AreaData(Position,2)       
            Case 2: temp = VolumeData(Position,2)   
            Case 3: temp = MassData(Position,2)       
            Case 4: temp = SpeedData(Position,2)   
            Case 5: temp = PowerData(Position,2)
            Case 6: fromCountry = countries.get(Position)
    End Select
   
    fromCountry = fromCountry.SubString2(0, 3)                         'Gets 3 letter code representing the countries  currency  IE USD
   
    ConvertCommon = Disp / temp
    ConvFromView.RemoveView                                        'Exit ListView.
    lblTitle.Top = 0%y
    ListBack.Top = lblTitle.Top
    lblTitle.SendToBack
    ListBack.SendToBack
    CallSub("",NextConvTo)                                        'Sub CurrencyTo
End Sub


Sub ConvToView_ItemClick (Position As Int, ResultsValue As Object)
    Private temp As String
    toCountry = "ABC"
    Select AllData
        Case 0: Disp = LengthData(Position,3):  temp = LengthData(Position,1)
        Case 1:    Disp = AreaData(Position,3):    temp = AreaData(Position,1)   
        Case 2: Disp = VolumeData(Position,3):  temp = VolumeData(Position,1)   
        Case 3: Disp = MassData(Position,3):    temp = MassData(Position,1)       
        Case 4: Disp = SpeedData(Position,3):   temp = SpeedData(Position,1)
        Case 5: Disp = PowerData(Position,3):   temp = PowerData(Position,1)
        Case 6: toCountry = countries.get(Position)
    End Select
    toCountry = toCountry.SubString2(0, 3)
 
    If AllData <> 6 Then            'Do for all except Currency
        Buffer = ""
        StackPush
        send_to_display(Disp)
        If Two_flag = 1 Then
            Idisplay.Text = IDispTXT
            Two_flag = 0
        End If
        Idisplay.Text = temp
    Else
        FromTo = fromCountry&toCountry
        XRate.Download("http://finance.yahoo.com/d/quotes.csv?s="&FromTo&"=X&f=l1")   'Gets conversion rate between 2 currency's
    End If
   
    ConvToView.RemoveView                                    'Exit ListView.
    lblTitle.Top = 0%y
    ListBack.Top = lblTitle.Top
    lblTitle.SendToBack
    ListBack.SendToBack
    ListFlag = 0
    ConvToView.Initialize("ConvToView")
End Sub


Sub JobDone(job As HttpJob)
   If job.Success Then
        Private  Rate, FromDisp, ToDisp As String    'result,i, i2,
        Private ToResult, FromAmmount As Double
       
        FromAmmount = Rdisplay.Text
        Rate = job.GetString
       
        If Rate < 0 Then
            Msgbox("Invalid response.", "Error")
            Return
        End If
        If IsNumber(Rdisplay.Text) = False Then
            Msgbox("Please enter a valid number.", "Error")
            Return
        End If   
   
'        Set the answer
        FromDisp = Rdisplay.Text&"   "&fromCountry
        ToResult = Rate * FromAmmount
        ToResult = NumberFormat2(ToResult,1,2,2,False)
        ToDisp = ToResult&"   "&toCountry&"     "
        If ToResult = 0 Or ToResult < 0 Then ToDisp = "----   "&toCountry&"     "
        If IsNumber(ToResult) = False Then  ToDisp = "----   "&toCountry&"     "
        Rdisplay.Text = FromDisp
        Idisplay.Text = ToDisp
   Else
        ToastMessageShow ( "Error connecting to server.", True)
   End If
   job.Release
End Sub

Hopefully this will be helpful to those who follow in search of Yahoo currency.

Once again many thanks to those members above for the help, especially Erel and his code example.

Regards Roger
 
Upvote 0
Top