Android Tutorial Currency Converter - Http web services and more

Widget

Well-Known Member
Licensed User
Longtime User
Consider using the new HttpUtils framework for simpler handling of web services: http://www.b4x.com/forum/showthread.php?p=109068

Does anyone have this example working with the latest OkHttpUtils2 library? I modified the code to use HttpJob but whatever value I try to convert, the website just returns the negative value of what I send it. I'm using version 5.80.

B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: Currency Converter
    #VersionCode: 1
    #VersionName: 
    #SupportedOrientations: unspecified
#End Region

'Activity module
Sub Process_Globals
    Dim countries As List
    Dim URL As String
    URL = "http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency="
    'Dim HttpClient1 As OkHttpClient
    Type StateType (TextUp As String, TextDown As String, _
        IndexUp As Int, IndexDown As Int)
    Dim State As StateType 'This must be a process variable as it stores the state
                            'and should not be released when the activity is destroyed.
End Sub

Sub Globals
    Dim txtUp, txtDown As EditText
    Dim spnrUp, spnrDown As Spinner
    Dim btnUp, btnDown As Button
    Type MyTag (FromValue As EditText, ToValue As EditText, _
                FromCurrency As Spinner, ToCurrency As Spinner)
    Dim CurrentTask As MyTag
End Sub
Sub ResetState
    'set the starting state
    State.TextUp = 1
    State.TextDown = ""
    State.IndexUp = 0 'USD
    State.IndexDown = 43 'Euro
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        Log("************************")
        'load the list of countries
        countries = File.ReadList(File.DirAssets, "CountryCodes.txt")
        'initialize the HttpClient object which is responsible for all communication.
        'HttpClient1.Initialize("HttpClient1")
        ResetState
    End If
   
    Activity.LoadLayout("layout1")
    spnrUp.AddAll(countries)
    spnrDown.AddAll(countries)
   
    Dim t1 As MyTag
    t1.FromValue = txtUp
    t1.ToValue = txtDown
    t1.FromCurrency = spnrUp
    t1.ToCurrency = spnrDown
    btnDown.Tag = t1
   
    Dim t2 As MyTag
    t2.FromValue = txtDown
    t2.ToValue = txtUp
    t2.FromCurrency = spnrDown
    t2.ToCurrency = spnrUp
    btnUp.Tag = t2
End Sub

Sub Activity_Resume
    txtUp.Text = State.TextUp
    txtDown.Text = State.TextDown
    spnrUp.SelectedIndex = State.IndexUp
    spnrDown.SelectedIndex = State.IndexDown
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        ResetState 'reset the state to the initial settings.
    Else
        State.TextUp = txtUp.Text
        State.TextDown = txtDown.Text
        State.IndexUp = spnrUp.SelectedIndex
        State.IndexDown = spnrDown.SelectedIndex
    End If
End Sub

Sub btn_Click
    Dim btn As Button
    btn = Sender 'Fetch the actual button that raised this event.
    CurrentTask = btn.Tag 'Take the object from its Tag property.
    Dim fromCountry, toCountry As String
    fromCountry = CurrentTask.FromCurrency.SelectedItem.SubString2(0, 3) 'get the currency code
    toCountry = CurrentTask.ToCurrency.SelectedItem.SubString2(0, 3)
   
'    Dim request As OkHttpRequest
'    request.InitializeGet(URL & fromCountry & "&ToCurrency=" & toCountry)
'    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).

  Private Job1 As HttpJob
    Job1.Initialize("Job1",Me)
    Job1.Download(URL & fromCountry & "&ToCurrency=" & toCountry)
   
    ProgressDialogShow("Calling server...")
End Sub

Sub JobDOne(Job As HttpJob)
    Private Result As String

  ProgressDialogHide

    Log("JobDone")
   
    Log("JobName="&Job.JobName & ", success=" & Job.Success)
    If Job.Success = True Then
        Log("Success="&Job.GetString)
        Result = Job.GetString
       
        Dim rate As Double
        Dim i, i2 As Int
        '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)
        Log("Rate = " & rate)
        If IsNumber(CurrentTask.FromValue.Text) = False Then
            Msgbox("Please enter a valid number.", "Error")
            Return
        End If
        'Set the answer
       
        CurrentTask.ToValue.Text = Round2(CurrentTask.FromValue.Text * rate, 2)
    End If
End Sub

'Sub HttpClient1_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
'    Log("ResponseSuccess")
'    ProgressDialogHide
'    Dim result As String
'    result = Response.GetString("UTF8") 'Convert the response to a string
'    Log(result)
'    Dim 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)
'    Log("Rate = " & rate)
'    If IsNumber(CurrentTask.FromValue.Text) = False Then
'        Msgbox("Please enter a valid number.", "Error")
'        Return
'    End If
'    'Set the answer
'   
'    CurrentTask.ToValue.Text = Round2(CurrentTask.FromValue.Text * rate, 2)
'End Sub

'Sub HttpClient1_ResponseError (Reason As String, StatusCode As Int, TaskId As Int)
'    Log(Reason)
'    Log(StatusCode)
'    ProgressDialogHide
'    msg = "Error connecting to server."
'    If Reason <> Null Then msg = msg & CRLF & Reason
'    ToastMessageShow (msg, True)
'End Sub

Is it my code or the website?
TIA
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…