SOAP Web Service

csgoh

Member
Licensed User
Longtime User
Hi, I am trying to invoke a web service operation, but I have no luck so far.

I have tested the SOAP-based web service using SOAPUI and this is the SOAP request XML that the web service is expecting


I have tested it in SOAPUI, it works perfectly, here is the HTTP log:

And this is my B4A codes:


B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim webClient As HttpClient
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.

   Dim btnLogin As Button
   Dim pnlDM As Panel
   
   Dim webRequest As HttpRequest
   
   Dim lblLoginStatus As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("MainScreen")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub btnLogin_Click
   Dim endPoint As String
   Dim requestSoapXML As String
   
   
   Dim domain, userName, password, library As String
   
   domain = "mydomain"
   userName = "testuser1"
   password = "password"
   library = "LIB"
   
   endPoint = "http://soa61.mydomain.com:9080/services/MyService"
   
   requestSoapXML = _
         "<soapenv:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ejb='http://ejb.dm.service.abc.nz'>" & _ 
            "<soapenv:Header/>" & _
         "<soapenv:Body>" & _
            "<ejb:doInitialLogin soapenv:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>" & _
               "<arg_0_15 xsi:Type='xsd:String'>" & domain & "</arg_0_15>" & _
               "<arg_1_15 xsi:Type='xsd:String'>" & userName & "</arg_1_15>" & _
               "<arg_2_15 xsi:Type='xsd:String'>" & password & "</arg_2_15>" & _
               "<arg_3_15 xsi:Type='xsd:String'>" & library & "</arg_3_15>" & _
            "</ejb:doInitialLogin>" & _
         "</soapenv:Body>" & _
         "</soapenv:Envelope>"
   Log(requestSoapXML )
   
   webRequest.InitializePost2(endPoint, requestSoapXML .GetBytes("UTF8"))
   webRequest.SetHeader("Content-Type", "application/soap+xml; charset=utf-8")
   webRequest.Timeout = 10000 

   
   webClient.Initialize("webClient")
   If webClient.Execute(webRequest, 1) = False Then Return
End Sub

Sub webClient_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   Dim resultSoapXML As String
   
   resultSoapXML = Response.GetString("UTF8")
   lblLoginStatus.Text = resultSoapXML
   Log("Success : " & resultSoapXML)
End Sub

Sub webClient_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
   Dim resultSoapXML As String
   
   resultSoapXML = Response.GetString("UTF8")
   lblLoginStatus.Text = resultSoapXML
   Log("Error : " & resultSoapXML)
   Log("Reason: " & Reason)
   Log("StatusCode: " & StatusCode)
End Sub

But I always get a Client.NoSOAPAction Error as below:


Did I miss something?

Appreciate any help.

:sign0085:
 
Last edited:

csgoh

Member
Licensed User
Longtime User
Thanks! It works by adding one additional line.

B4X:
webRequest.SetHeader("SOAPAction", "")
 
Upvote 0

paolofi

Member
Licensed User
Longtime User
I trying to consume an Apache AXIS webserver (HTTPS), and I modified the code above with my parameters, the first part of initialization connection flow fine but when read the result I receive this error:


An error has occurred in sub:main_webclient_responserror (B4A line 95)
resultSoapXML = Response.GestString("UTF8)
java.lang.NullPointerException


at this lines:

B4X:
Sub webClient_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
   Dim resultSoapXML As String
   
   resultSoapXML = Response.GetString("UTF8")
   lblLoginStatus.Text = resultSoapXML
   Log("Error : " & resultSoapXML)
   Log("Reason: " & Reason)
   Log("StatusCode: " & StatusCode)
End Sub

If a use:
B4X:
webClient.InitializeAcceptAll("webClient")

instead of:
B4X:
webClient.Initialize("webClient")

the error move to sub:
B4X:
Sub webClient_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   Dim resultSoapXML As String
   
   resultSoapXML = Response.GetString("UTF8")
   lblLoginStatus.Text = resultSoapXML
   Log("Success : " & resultSoapXML)
End Sub

and become:

An error has occurred in sub:main_webclient_responserror (B4A line 87)
resultSoapXML = Response.GetString("UTF8)"
android.os.NetworkOnMainThreadException


I am a very noob in java, wich problem can generate this error?

Thanks


Paolo
 
Last edited:
Upvote 0

paolofi

Member
Licensed User
Longtime User
You should not use Response.GetString. This method is deprecated.

You should instead use HttpUtils2 (or Response.GetAsynchronously).

Unfortunately this is the only example I found that match my needs (WSDL+XML parameters) so I tried to adapt it.
It's no clear for me the how to use HttpUtils2 to send xml parameters.

I tried to convert it to HttpUtils2 flow:
....
B4X:
Dim job As HttpJob

requestSoapXML = "<soapenv:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ejb='http://ejb.dm.service.abc.nz'>" & _ 
            "<soapenv:Header/>" & _
         "<soapenv:Body>" & _
            "<ejb:ServiceName soapenv:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>" & _
               "<par1 xsi:Type='xsd:String'>" & varPar1 & "</par1>" & _
               "<par2 xsi:Type='xsd:String'>" & varPar2 & "</par2>" & _
               "<par3 xsi:Type='xsd:String'>" & varPar3 & "</par3>" & _
            "</ejb:ServiceName>" & _
         "</soapenv:Body>" & _
         "</soapenv:Envelope>"

job.Initialize("JOB1", Me)
job.PostString ("https://xxx.xxx.xxx.xxx/services/ServicesNames", requestSoapXML)

job.PostString is the right method to pass the XML parameters string to webserver?

Regards
 
Upvote 0

paolofi

Member
Licensed User
Longtime User

Post this my cut&paste working solution hoping to help somebody:

I was not able to convert this to HttpUtils2 I'm grateful to those who will succeed to do so.:sign0104:

B4X:
'use HTTP 1.22 library

Sub Process_Globals
    Dim webClient As HttpClient
End Sub

Sub Globals
    Dim webRequest As HttpRequest
End Sub

Sub btnSend_Click
    Dim endPoint As String
    Dim requestSoapXML As String

    endPoint = "http://webserversite"

   requestSoapXML = _
         "<soapenv:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ejb='http://ejb.dm.service.abc.nz'>" & _ 
            "<soapenv:Header/>" & _
         "<soapenv:Body>" & _
            "<ejb:ServiceName soapenv:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>" & _
               "<par1 xsi:Type='xsd:String'>" & varPar1 & "</par1>" & _
               "<par2 xsi:Type='xsd:String'>" & varPar2 & "</par2>" & _
               "<par3 xsi:Type='xsd:String'>" & varPar3 & "</par3>" & _
            "</ejb:ServiceName>" & _
         "</soapenv:Body>" & _
         "</soapenv:Envelope>"
   
   webRequest.InitializePost2(endPoint, requestSoapXML .GetBytes("UTF8"))
   webRequest.SetHeader("Content-Type", "application/soap+xml; charset=utf-8")
   webRequest.Timeout = 20000 
   webRequest.SetHeader("SOAPAction", "")
   
   webClient.InitializeAcceptAll("webClient")
   
   If webClient.Execute(webRequest, 1) = False Then Return
End Sub

Sub webClient_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   Dim resultSoapXML As String
    my_buffer.InitializeToBytesArray(5000) ' I expect less than 2000 bytes here    
   Response.GetAsynchronously("Response", my_buffer, True, TaskId)   
End Sub

Sub webClient_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
   Dim resultSoapXML As String
   resultSoapXML = Response.GetString("UTF8")
End Sub

Sub Response_StreamFinish (Success As Boolean, TaskId As Int)    
   Dim resultSoapXML As String
   Dim another_buffer () As Byte    
   
   another_buffer = my_buffer.ToBytesArray    
   
   resultSoapXML=BytesToString(another_buffer, 0, another_buffer.Length, "UTF8")
   
   'Msgbox(resultSoapXML,"")
End Sub

Regards
 
Upvote 0

zeuspower

Member
Licensed User
Longtime User
OK... merry xmas first of all :icon_clap:

now...I am trying to get a webservice responce ,I use your code with the webservice setting that I want to get connected ,with an example data:

B4X:
'use HTTP 1.22 library

Sub Process_Globals
    Dim webClient As HttpClient
   
End Sub

Sub Globals
    Dim webRequest As HttpRequest
   Dim btnSend As Button
   Dim my_buffer As OutputStream
   
End Sub

Sub btnSend_Click
    Dim endPoint As String
    Dim requestSoapXML As String

    endPoint = "http://webserversite"

    requestSoapXML = "<soapenv:Envelope xmlns:soapenv=  'http://schemas.xmlsoap.org/soap/envelope/' xmlns:rgw= ' http://gr/gsis/rgwsbasstoixn/RgWsBasStoixN.wsdl' xmlns:typ= 'http://gr/gsis/rgwsbasstoixn/RgWsBasStoixN.wsdl/types/' xmlns:xsi= ' http://www.w3.org/2001/XMLSchema-instance' >"& _
 "<soapenv:Header/>"& _
   "<soapenv:Body>"& _
      "<rgw:rgWsBasStoixEpit>"& _
         "<pAfm xsi:Type='xsd:string'>090060693</pAfm>"& _
         "<pBasStoixEpitRec_out> "& _
            "<typ:residenceParDescription></typ:residenceParDescription> "& _
            "<typ:assTxpActualAfm></typ:assTxpActualAfm> "& _
            "<typ:actLongDescr></typ:actLongDescr> "& _
            "<typ:postalZipCode></typ:postalZipCode> "& _
            "<typ:INiFlagDescr></typ:INiFlagDescr> "& _
            "<typ:registDate>2011-01-01</typ:registDate> "& _
            "<typ:stopDate>2011-01-01</typ:stopDate> "& _
            "<typ:parDescription></typ:parDescription>"& _
           "<typ:doyDescr></typ:doyDescr> "& _
            "<typ:residenceZipCode></typ:residenceZipCode> "& _
            "<typ:deactivationFlagDescr></typ:deactivationFlagDescr> "& _
            "<typ:legalStatusDescr></typ:legalStatusDescr> "& _
            "<typ:firmPhone></typ:firmPhone> "& _
            "<typ:firmFax></typ:firmFax> "& _
            "<typ:afm></typ:afm> "& _
            "<typ:facActivity>0</typ:facActivity> "& _
            "<typ:countOfBranches>0</typ:countOfBranches> "& _
            "<typ:deactivationFlag>1</typ:deactivationFlag>"& _
            "<typ:postalAddressNo></typ:postalAddressNo> "& _
            "<typ:firmFlagDescr></typ:firmFlagDescr> "& _
            "<typ:postalAddress></typ:postalAddress> "& _
            "<typ:doy></typ:doy> "& _
            "<typ:onomasia></typ:onomasia> "& _
            "<typ:commerTitle></typ:commerTitle>"& _
         "</pBasStoixEpitRec_out> "& _
         "<pCallSeqId_out>0</pCallSeqId_out> "& _
         "<pErrorRec_out> "& _
           " <typ:errorDescr></typ:errorDescr> "& _
           " <typ:errorCode></typ:errorCode> "& _
         "</pErrorRec_out> "& _
     " </rgw:rgWsBasStoixEpit> "& _
     " </soapenv:Body> "& _
    "</soapenv:Envelope>"
    
    webRequest.InitializePost2(endPoint, requestSoapXML .GetBytes("UTF8"))
    webRequest.SetHeader("Content-Type", "application/soap+xml; charset=utf-8")
    webRequest.Timeout = 20000 
    webRequest.SetHeader("SOAPAction", "")
    
    webClient.InitializeAcceptAll("webClient")
    
    If webClient.Execute(webRequest, 1) = False Then Return
End Sub

Sub webClient_ResponseSuccess (Response As HttpResponse, TaskId As Int)
    Dim resultSoapXML As String
   
    my_buffer.InitializeToBytesArray(10000) ' I expect less than 2000 bytes here    
    Response.GetAsynchronously("Response", my_buffer, True, TaskId) 
   
End Sub

Sub webClient_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
    Dim resultSoapXML As String
    resultSoapXML = Response.GetString("UTF8")
   Log (resultSoapXML)
End Sub

Sub Response_StreamFinish (Success As Boolean, TaskId As Int)    
    Dim resultSoapXML As String
    Dim another_buffer () As Byte    
    
    another_buffer = my_buffer.ToBytesArray 
    
    resultSoapXML=BytesToString(another_buffer, 0, another_buffer.Length, "UTF8")
    
    Msgbox(resultSoapXML,"")
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("main") ' a simple layout with just one button

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

I am getting :

main_webclient_responseerror (java line: 334)
java.lang.NullPointerException


I don't know if it is something simple or I have mess up the XML syntax :signOops:


Any help-correction-advise gratly appreciated :sign0104:
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
As I previously recommended, you should use HttpUtils2 instead of HttpClient directly. It will be simpler and will work better. The problem in this case is that Response is probably not initialized in webClient_ResponseError. It is not initialized if there was not response from the server.
Switching to HttpUtils2 will solve this issue as well as others.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…