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, '') ):
and response with token
The second "method" request 3 parameter ( s2.paline.Previsioni (token, '70101', 'en')
and response with timer, distance,etc...
Code in B4A for first "method":
The structure to pass the two parameters is this:
Server response with token.
Code in B4A for second "method":
The structure to pass the three parameters is this:
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:
With this code we have result in JSON ( See Class XML2Map )
Have nice day
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:
Python
The following example shows how to authenticate with the API Getting around Rome (you must have a key developer), and invoke a method. You can simply paste the code below into a Python shell, after the necessary amendments to variable DEV_KEY.
try:
# 2 Python import
from xmlrpclib import Server
except ImportError:
# 3 Python import
from xmlrpc.client import Server
from pprint import pprint
DEV_KEY = 'Insert your key'
s1 = Server ('http://muovi.roma.it/ws/xml/autenticazione/1')
s2 = Server ('http://muovi.roma.it/ws/xml/paline/7')
token = s1.autenticazione.Accedi (DEV_KEY, '')
res = s2.paline.Previsioni (token, '70101', 'en')
pprint (res)
Other languages
The use of the API in other languages is similar to the case of Python. You need to download a library XML-RPC for its language and to study the documentation.
Developers are invited to share code samples in different programming languages.
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: