Android Question How to convert this code to b4a

Makumbi

Well-Known Member
Licensed User
Please help how can i covert this vb code to b4a thanks in advance

B4X:
Private Async Sub Btnsubmit_OnClicked(ByVal sender As Object, ByVal e As EventArgs)
        Dim ac As Subscriptiondetails = New Subscriptiondetails
        Dim oldphone As String = phone.Text
        If (phone.Text.StartsWith("0") = true) Then
            Dim str As String = phone.Text
            str = ("256" + str.Remove(0, 1))
            phone.Text = str
        End If
        
        ac.username = "5"
        ac.password = "ty3"
        ac.api = "depositmobilemoney"
        ac.msisdn = phone.Text
        ac.amount = 10000
        ac.narration = "Subscription"
        ac.reference = "sbscr"
        ac.status_notification_url = "http://108.175.14.205/SMISApi/api/smis/subscriptionresponse"
        Dim json = JsonConvert.SerializeObject(ac)
        Dim content = New StringContent(json, Encoding.UTF8, "application/Json")
        Dim client As HttpClient = New HttpClient
        Dim result As HttpResponseMessage = client.PostAsync("https://payments-dev.blink.co.ug/api/", content)
        If result.IsSuccessStatusCode Then
            Dim data = result.Content.ReadAsStringAsync
            Dim product = JsonConvert.DeserializeObject(Of ResponseexternalApi)(data)
            Dim y As String = product.status.ToString
            Dim z As String = product.error.ToString
            Dim w As String = product.reference_code.ToString
            If ((y = "PENDING")  _
                        AndAlso (z = "false")) Then
                DisplayAlert("Hey!", "Yr subscription IS BEING WORKED UPON THANK YOU", "Alright")
            End If
            
        End If
        
    End Sub
 

mcqueccu

Well-Known Member
Licensed User
Longtime User
This and B4x is 99.999999% the similar and should not be difficult at all.

Am not with my PC but do and post your code so that we guide you.
 
Upvote 0

mcqueccu

Well-Known Member
Licensed User
Longtime User
This is untested code - Try it and give feedback

B4X:
Sub Btnsubmit_Click
      
    Dim oldphone As String = phone.Text
    If phone.Text.StartsWith("0")  Then
        Dim str As String = phone.Text
        str = "256" & str.SubString(1)
        phone.Text = str
    End If
                
    Dim Subscriptiondetails As Map
    Subscriptiondetails.Initialize
    
    Subscriptiondetails.put("username","5")
    Subscriptiondetails.put("password", "ty3")
    Subscriptiondetails.put("api",depositmobilemoney)
    Subscriptiondetails.put("msisdn",phone.Text)
    Subscriptiondetails.put("amount",10000)
    Subscriptiondetails.put("narration",Subscription)
    Subscriptiondetails.put("reference",sbscr)
    Subscriptiondetails.put("status_notification_url","http://108.175.14.205/SMISApi/api/smis/subscriptionresponse")
        
    'Depends on json library
    Dim json As JSONGenerator
    json.Initialize(Subscriptiondetails)
    Dim content As String = json.ToString
        
    'Depends on okHttputils library
    Dim client As HttpJob
    client.Initialize("",Me)
        
    client.PostString("https://payments-dev.blink.co.ug/api/",content)
    client.GetRequest.SetContentType("application/json")    'set the header as json
    client.GetRequest.SetContentEncoding("UTF8")        'set encoding as utf8
        
    Wait For Jobdone(client As HttpJob)
    
    If client.Success Then
        Dim result As String = client.GetString
        Dim data As JSONParser
        data.Initialize(result)   
        
        Dim product As Map = data.NextObject    'I suspect the response is a json object
        
        Dim y As String = product.Get("status")
        Dim z As String = product.Get("error")
        Dim w As  String = product.Get("reference_code")
        
        If y = "PENDING" And z = "false" Then
            MsgboxAsync("Hey!, Yr subscription IS BEING WORKED UPON THANK YOU","Alright")
        End If
        
    End If

      
End Sub
 
Upvote 0
Top