Android Code Snippet Manage XMLRPC with okHttpUtils easily (B4X)

Hi all.
I see that there are often questions if there are XMLRPC libraries working in B4X ( B4A, B4i ).
Managing the same is very simple through okHttputils.
Let's go...
I have a site that work with XMLRPC
In the site i read:

The first "method" request 2 parameter ( s1.autenticazione.Accedi (DEV_KEY, '') ):

- DEV_KEY
- ''

and response with token

The second "method" request 3 parameter ( s2.paline.Previsioni (token, '70101', 'en')
- token
- '70101' ( or another number bus )
- 'en' ( language)

and response with timer, distance,etc...

Code in B4A for first "method":
B4X:
    'GET TOKEN
    Dim requestSoapXML1 As String = $"<?xml version='1.0'?>
<methodCall>
<methodName>autenticazione.Accedi</methodName>
<params>
<param>
    <value>QCOT7MBQwaC8dQI3BcVEC58kTCeriOOi</value>
    <value>''</value>
</param>
</params>
</methodCall>"$

    Dim job1 As HttpJob
    job1.Initialize("Autentico", Me)
    job1.PostString ("http://muovi.roma.it/ws/xml/autenticazione/1", requestSoapXML1)

The structure to pass the two parameters is this:

....
<value>QCOT7MBQwaC8dQI3BcVEC58kTCeriOOi</value>
<value>''</value>
.....

Server response with token.

Code in B4A for second "method":

B4X:
    'GET DATA
                Dim requestSoapXML2 As String = $"<?xml version='1.0'?>
<methodCall>
<methodName>paline.Previsioni</methodName>
<params>
<param>
        <value>${token}</value>
        <value>70101</value>
        <value>''</value>
</param>
</params>
</methodCall>"$

Log("Request2: " & requestSoapXML2)
                Dim job1 As HttpJob
                job1.Initialize("Orari", Me)
                job1.PostString ("http://muovi.roma.it/ws/xml/paline/7", requestSoapXML2)


The structure to pass the three parameters is this:

....
<value>${token}</value>
<value>70101</value>
<value>''</value>
.....

so if you have a method that requires n. parameters, just add the same.

Obviously the server will return the answers in xml format, we can turn them into a somewhat simpler (Json result), easier to manage. This is code complete:

B4X:
#Region  Project Attributes
    #ApplicationLabel: Bus Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private ParsedData As Map
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    'GET TOKEN
    Dim requestSoapXML1 As String = $"<?xml version='1.0'?>
<methodCall>
<methodName>autenticazione.Accedi</methodName>
<params>
<param>
    <value>QCOT7MBQwaC8dQI3BcVEC58kTCeriOOi</value>
    <value>''</value>
</param>
</params>
</methodCall>"$

    Dim job1 As HttpJob
    job1.Initialize("Autentico", Me)
    job1.PostString ("http://muovi.roma.it/ws/xml/autenticazione/1", requestSoapXML1)

End Sub

Private Sub JobDone(job As HttpJob)

    If job.Success Then
        Dim res As String
        res = job.GetString
        Log("Response from server XML: " & res)
      
        'XML TO JSON
        Dim xm As Xml2Map
        xm.Initialize
        ParsedData = xm.Parse(res)  
        Dim jg As JSONGenerator
        jg.Initialize(ParsedData)
        Log(jg.ToPrettyString(1))
      
      
        Dim parser As JSONParser
        Select job.JobName
            Case "Autentico"          
                parser.Initialize(jg.ToPrettyString(1))
                Dim root As Map = parser.NextObject
                Dim methodResponse As Map = root.Get("methodResponse")
                Dim params As Map = methodResponse.Get("params")
                Dim param As Map = params.Get("param")
                Dim value As Map = param.Get("value")
                Dim token As String = value.Get("string")
                Log("Token: " & token)
                'GET DATA
                Dim requestSoapXML2 As String = $"<?xml version='1.0'?>
<methodCall>
<methodName>paline.Previsioni</methodName>
<params>
<param>
        <value>${token}</value>
        <value>70101</value>
        <value>''</value>
</param>
</params>
</methodCall>"$

Log("Request2: " & requestSoapXML2)
                Dim job1 As HttpJob
                job1.Initialize("Orari", Me)
                job1.PostString ("http://muovi.roma.it/ws/xml/paline/7", requestSoapXML2)
          
          
            Case "Orario"
                '.....
              
              
          
          
        End Select
Else
    'Gestione errore su internet
    Log("Error: " & job.ErrorMessage)
End If
    job.Release
End Sub


Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

With this code we have result in JSON ( See Class XML2Map )

B4X:
....
        Dim res As String
        res = job.GetString
        Log("Response from server XML: " & res)
     
        'XML TO JSON
        Dim xm As Xml2Map
        xm.Initialize
        ParsedData = xm.Parse(res) 
        Dim jg As JSONGenerator
        jg.Initialize(ParsedData)
        Log(jg.ToPrettyString(1))
.....

Have nice day
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…