web service consume in b4a

lagore

Active Member
Licensed User
Longtime User
Web service

Hi,
this is easy using a Http POST
B4X:
Sub runwebservice2
    Dim Url1 As String
    Dim requestSoapXML As String
    Dim tempC As String
    
    tempC = "25"
   
      Url1 = "http://www.w3schools.com/webservices/tempconvert.asmx"
   
    requestSoapXML = _
            "<?xml version='1.0' encoding='utf-8'?>" & _ 
               "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" & _
                  "<soap:Body>" & _
                      "<CelsiusToFahrenheit xmlns='http://tempuri.org/'>" & _
                          "<Celsius>" & tempC & "</Celsius>" & _
                         "</CelsiusToFahrenheit>" & _
                  "</soap:Body>" & _
               "</soap:Envelope>"
    Log(requestSoapXML )
    
    webRequest.InitializePost2(Url1, requestSoapXML.GetBytes("UTF8"))
    webRequest.SetHeader("Content-Type", "text/xml; charset=utf-8")
   webRequest.SetHeader("SOAPAction", "http://tempuri.org/CelsiusToFahrenheit")
   
    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")
   edittext1.Text = resultSoapXML
    '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
I have an EDITTEXT (edittext1) on my form and the result is displayed on it. All you need to do with "resultSoapXML" in the success sub is parse the XML for the result temperature.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Sub Process_Globals
   Dim PostUrl As String
   PostUrl = "http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit"
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   HttpUtils.CallbackActivity = "Main"
   HttpUtils.CallbackJobDoneSub = "JobDone"
   HttpUtils.PostString("POST Job1", PostUrl, "Celsius=123")
End Sub

Sub Activity_Resume
   'Check whether a job has finished while the activity was paused.
   If HttpUtils.Complete = True Then JobDone(HttpUtils.Job)
End Sub

Sub JobDone (Job As String)
   If HttpUtils.IsSuccess(PostUrl) Then
      Msgbox(HttpUtils.GetString(PostUrl), "")
   Else
      ToastMessageShow("Error sending request", True)
   End If
   HttpUtils.Complete = False 'Turn off the complete flag so we won't handle it again if the activity is resumed.
End Sub

You will need to add HttpUtils and HttpUtilsService to your project.
I didn't parse the result. It should be pretty simple.
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
Hi Erel,
You are absolutely correct about using HttpUtils as the best option I happen to have an old SOAP template in a test program that I used, to explain to 'coskunor' why our methods are different Earl's is a pure HTTP POST (3rd option on the web page) whereas mind is a SOAP POST (1st option), in this case Earl's is the simplest to use but both give the same result. Quite often the SOAP is the only option.
 
Upvote 0

coskunor

Member
Licensed User
Longtime User
web service

Thank you both of you i will try in my case and inform you,
thanks again for your help...
 
Upvote 0

coskunor

Member
Licensed User
Longtime User
Need more help

I have used both methods lagore's and Erel's, and both were
very successful,
but when i used those in my other web service got below;

Error : Server did not recognize the value of HTTP Header
Reason : Internal Server Error
StatusCode : 500


The only difference from CelsiustoFahrenheitConversion service is;
Returns a three column dataset.
web service works for iphone and windows mobile 6.5 perfectly,
could you please give me any hint...

thanks,
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
Do you have an example of the other service post details, I presume you modified the headers to suit.

Sent from my HTC Desire using Tapatalk 2
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
Hi not to sure what was wrong with your code but the following works,
B4X:
Sub runwebservice3
    Dim Url1 As String
    Dim requestSoapXML As String
    Dim tempC As String
    
'   HttpUtils.PostString("POST Job1", PostUrl, "Celsius=123")
   
   
    tempC = "35412003678149801"
   
      Url1 = "http://212.58.21.132:81/BusinessService/IsTakipService.asmx"
   
    requestSoapXML = _
            "<?xml version='1.0' encoding='utf-8'?>" & _ 
               "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" & _
                  "<soap:Body>" & _
                      "<Login_ByImei xmlns='http://www.fu.com.tr/Webservices'>" & _
                          "<paramImeiNo>" & tempC & "</paramImeiNo>" & _
                         "</Login_ByImei>" & _
                  "</soap:Body>" & _
               "</soap:Envelope>"
    Log(requestSoapXML )
    
    webRequest.InitializePost2(Url1, requestSoapXML.GetBytes("UTF8"))
    webRequest.SetHeader("Content-Type", "text/xml; charset=utf-8")
   webRequest.SetHeader("SOAPAction", "http://www.fu.com.tr/Webservices/Login_ByImei")
   
    webRequest.Timeout = 10000 

    
    webClient.Initialize("webClient")
    If webClient.Execute(webRequest, 1) = False Then Return
End Sub
by the way don't forget to put code tags around your code to make it more readable.
 
Upvote 0

coskunor

Member
Licensed User
Longtime User
Hi Erel,

Sorry but could not find a user guide to use Httputils for another web service
with two string input parameters and caling a function from four different
functions in the same ..asmx

where shall i put function name and the second string parameter in similar sub listed below.

Thank you very much

Best regards


Sub Activity_Create(FirstTime As Boolean)
HttpUtils.CallbackActivity = "Main"
HttpUtils.CallbackJobDoneSub = "JobDone"
HttpUtils.PostString("POST Job1", PostUrl, "Celsius=123")
End Sub
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
Not sure if this is what you are wondering but you need to add a 'Select' to the JobDone sub in Erel's example to catch more then one request.
B4X:
Sub JobDone (Job As String)
   Select Job
      Case "POST Job1"   
         If HttpUtils.IsSuccess(PostUrl) Then
              Msgbox(HttpUtils.GetString(PostUrl), "")
          Else
              ToastMessageShow("Error sending request", True)
          End If
      Case "POST Job2"
'your code here to handle Job2 return
   End Select

    HttpUtils.Complete = False 'Turn off the complete flag so we won't handle it again if the activity is resumed.
End Sub
 
Upvote 0

mriethoff

New Member
Licensed User
Longtime User
Sorry to use this thread but I tried editing you code, and get a java.nullpointer error

The Soap part comes from SOAPUI and is working

any ideas?
I think it has something to do with the webRequest.SetHeader("SOAPAction", "") part as I dont know for sure what to put in the URI.

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 resultSoapXML As String
    
    
    Dim userName, password As String
    

    userName = "Login1"
    password = "password1"

    endPoint = "http://servername:8080/test/Test/"
   
requestSoapXML = _ 
   "<soapenv:Envelope xmlns:soapenv=" & QUOTE & "http://schemas.xmlsoap.org/soap/envelope/" & QUOTE & " xmlns:urn=" & QUOTE & "urn:System:System" & QUOTE & ">" & _ 
      "<soapenv:Header/>" & _ 
         "<soapenv:Body>" & _ 
            "<urn:login>" & _ 
               "<urn:Login>"& userName & "</urn:Login>" & _ 
               "<urn:password>"& password & "</urn:password>" & _ 
            " </urn:login>" & _ 
         "</soapenv:Body>" & _ 
   "</soapenv:Envelope>"
    Log(requestSoapXML )
   
   webRequest.InitializePost2(endPoint, requestSoapXML .GetBytes("UTF8"))
    webRequest.Timeout = 10000 
   webRequest.SetHeader("Content-Type", "text/xml; charset=utf-8")
    webRequest.SetHeader("SOAPAction", "")

    
    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
 
Last edited:
Upvote 0

Tjunas

Member
Licensed User
Longtime User
Hi All,

what libraries are required to user the

B4X:
webRequest.InitializePost2(Url1, requestSoapXML.GetBytes("UTF8"))


I am trying to build a SOAP call but I keep getting errors.
 
Upvote 0
Top