how call a web services in B4A???

k1988

Member
Licensed User
Longtime User
HTTPUtils is a library that "facilitates" largely calls to web services ... or so mentioned. But the examples do not help someone who started with the library.

My problem is as follows:

I have a web service that accepts a string as a parameter and returns a List of Strings.

1 .- As I call my web service?
2 .- Where declare and sent my request?
3 .- How to get a list of strings or any object (such a class of)?

I expect prompt response, if possible a much better example code.
Thanks
 

k1988

Member
Licensed User
Longtime User
Oh, thanks :icon_clap:
I'll check that... I guess that it's some same with .Net services

By the way, not exists examples with HTTPUtils???
Only for curiously
 
Upvote 0

k1988

Member
Licensed User
Longtime User
There are two examples attached in the link you posted in the first post.
I recommend you to go over the HttpUtils tutorial and examples. If it doesn't work for you please post your code and we will help you.
Yeah, i look that but i don't understand how use correctly...
The tutorial not response anyone of my questions in this topic ^^u

1 .- As I call my web servicefrom...?
2 .- Where declare and sent my request. . . with parameter or without?
3 .- How to get a list of strings or any object (such a class of, or one structure)?

Something simple answer it's ok
 
Upvote 0

k1988

Member
Licensed User
Longtime User
Ok... let's do it x)

This XML it's from my web service

B4X:
 <?xml version="1.0" encoding="utf-8" ?> 
- <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://142.114.2.131/Sitios/">
  <string>Ericka</string> 
  <string>Martin</string> 
  <string>Damian</string> 
  <string>Juliana</string> 
  </ArrayOfString>

And this it's my code:

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim PostUrl As String 
   PostUrl = "http://142.114.2.131/Sitios/HolaMundo/HolaMundo.asmx?op=Lista"
   Dim parser As SaxParser 
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 Lista As ListView
   Dim TxtBox1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   HttpUtils.CallbackActivity = "Main"   
End Sub

Sub Activity_Resume
   If HttpUtils.Complete = True Then JobDone(HttpUtils.Job)
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub TxtBox1_EnterPressed
   Dim parametro As String
   parametro = TxtBox1.Text 
   LlamarFuncion(parametro)
End Sub

Sub LlamarFuncion(parametro As String)
   ProgressDialogShow("Buscando. . .")
   HttpUtils.PostString("POST Job1", PostUrl, parametro)
   HttpUtils.CallbackJobDoneSub = "JobDone"
   HttpUtils.CallbackUrlDoneSub = "UrlDone"
End Sub

Sub UrlDone(Url As String)
   Log(Url & " done")
   ProgressDialogHide
End Sub

Sub JobDone (Job As String)
   Select Job
      Case "POST Job1"
      Dim cadena As InputStream 
         If HttpUtils.IsSuccess(PostUrl) Then
         cadena =  HttpUtils.GetInputStream(PostUrl)
            'Log(HttpUtils.GetString("http://www.b4x.com/print.php"))
            parser.Parse(cadena, "Parser")
            cadena.Close 
         End If
   End Select
   HttpUtils.Complete = False 'Turn off the complete flag so we won't handle it again if the activity is resumed.
End Sub

Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
      
   For i = 0 To parser.Parents.Size 
   Lista.AddSingleLine(parser.Parents.Get(i))
   Next

End Sub

I believe that it's right, but not fill the ListView... what it's wrong? ^^U

Edit:
Error 500 in Web Server,

I check that and change the link URL to localhost but another error appear: http://localhost refused
 
Last edited:
Upvote 0

k1988

Member
Licensed User
Longtime User
Error 500 means that the server has encountered some error. It might be that you are sending the wrong values. As your server is not publicly accessible I cannot know why it happens.
The service only accept one String as parameter... but, it's ok the code?
Don't I forget anything? ^^u
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Your code looks fine. You can remove the UrlDone sub and hide the progress dialog in JobDone. UrlDone is useful when you have multiple Urls in a single job. In fact there is a bug in your code as UrlDone will not fire if there was an error. It only fires for successful Urls. In the case of an error the progress dialog will not be hidden.
 
Upvote 0

k1988

Member
Licensed User
Longtime User
Ok
I'll post if I have another question or error appear.

Thanks Erel
 
Upvote 0

k1988

Member
Licensed User
Longtime User
Ok... if I use the next line:

PostUrl = "http://142.114.2.131/Sitios/HolaMundo/HolaMundo.asmx?op=Lista"
The error said:

B4X:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code>

<soap:Reason><soap:Text xml:lang="en">
[B]Server was unable to process request. --> Data at the root level is invalid. Line 1, position 1.[/B]

</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>

But if I use:

PostUrl = "http://142.114.2.131/Sitios/HolaMundo/HolaMundo.asmx/Lista"
A Runtime Error appear.

Is it problem of my service?* or is it something that I can change from the HTTPUtils library?
Some idea???

*The web service returns a ArrayofString
 
Upvote 0

k1988

Member
Licensed User
Longtime User
Resume Of My Problem

My Service:

B4X:
<%@ WebService Language="VB" Class="HolaMundoWebSVB" %>


<WebService( _
    Namespace:="http://142.114.2.131/Sitios/", _
    Description:="Hola, aqui puedes observar los primeros Web Services creados por Fabian Garcia :D")> _
Public Class HolaMundoWebSVB

<WebMethod(Description:="Devuelve una lista")> _
    Public Function Lista(ByVal parametro As String) As List(Of String)
        
        Dim a As New List(Of String)
        a.Add("Fabian")
        a.Add("Eduardo")
        a.Add("Victor")
        a.Add("Alicia")
        a.Add("Iliana")
        
        If parametro = "Team" Then
            Return a
        End If
        
        Dim b As New List(Of String)
        b.Add("Ericka")
        b.Add("Martin")
        b.Add("Damian")
        b.Add("Juliana")
 
        Return b
    End Function

End Class

The response:

B4X:
 <?xml version="1.0" encoding="utf-8" ?> 
- <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://142.114.2.131/Sitios/">
  <string>Ericka</string> 
  <string>Martin</string> 
  <string>Damian</string> 
  <string>Juliana</string> 
  </ArrayOfString>

My code with HTTPUtils:

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim PostUrl As String 
   PostUrl = "http://142.114.2.131/Sitios/HolaMundo/HolaMundo.asmx?op=Lista"
   Dim parser As SaxParser 
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 Lista As ListView
   Dim TxtBox1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   HttpUtils.CallbackActivity = "Main"   
End Sub

Sub Activity_Resume
   If HttpUtils.Complete = True Then JobDone(HttpUtils.Job)
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub TxtBox1_EnterPressed
   Dim parametro As String
   parametro = TxtBox1.Text 
   LlamarFuncion(parametro)
End Sub

Sub LlamarFuncion(parametro As String)
   ProgressDialogShow("Buscando. . .")
   HttpUtils.PostString("POST Job1", PostUrl, parametro)
   HttpUtils.CallbackJobDoneSub = "JobDone"
   HttpUtils.CallbackUrlDoneSub = "UrlDone"
End Sub

Sub UrlDone(Url As String)
   Log(Url & " done")
   ProgressDialogHide
End Sub

Sub JobDone (Job As String)
   Select Job
      Case "POST Job1"
      Dim cadena As InputStream 
         If HttpUtils.IsSuccess(PostUrl) Then
         cadena =  HttpUtils.GetInputStream(PostUrl)
            'Log(HttpUtils.GetString("http://www.b4x.com/print.php"))
            parser.Parse(cadena, "Parser")
            cadena.Close 
         End If
   End Select
   HttpUtils.Complete = False 'Turn off the complete flag so we won't handle it again if the activity is resumed.
End Sub

Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
      
   For i = 0 To parser.Parents.Size 
   Lista.AddSingleLine(parser.Parents.Get(i))
   Next

End Sub

The error:

Your server expects a soap message. This is an xml message. You are sending it the wrong value. You should send an XML string as the parameter (the exact format depends on your server setttings).

I guess that here not having examples for doing that... right?

In Eclipse I tried usage ksoap2 library, but the library fail when catch the response...
and here (now) I have problems to send parameter (when I thought it would be more easy using B4A )

Maybe Android don't loves me xD

Well, i'll try thanks Erel
 
Upvote 0

k1988

Member
Licensed User
Longtime User
Resume Of My Problem:

My Service:

B4X:
<%@ WebService Language="VB" Class="HolaMundoWebSVB" %>


<WebService( _
    Namespace:="http://142.114.2.131/Sitios/", _
    Description:="Hola, aqui puedes observar los primeros Web Services creados por Fabian Garcia :D")> _
Public Class HolaMundoWebSVB

<WebMethod(Description:="Devuelve una lista")> _
    Public Function Lista(ByVal parametro As String) As List(Of String)
        
        Dim a As New List(Of String)
        a.Add("Fabian")
        a.Add("Eduardo")
        a.Add("Victor")
        a.Add("Alicia")
        a.Add("Iliana")
        
        If parametro = "Team" Then
            Return a
        End If
        
        Dim b As New List(Of String)
        b.Add("Ericka")
        b.Add("Martin")
        b.Add("Damian")
        b.Add("Juliana")
 
        Return b
    End Function

End Class

The response:

B4X:
 <?xml version="1.0" encoding="utf-8" ?> 
- <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://142.114.2.131/Sitios/">
  <string>Ericka</string> 
  <string>Martin</string> 
  <string>Damian</string> 
  <string>Juliana</string> 
  </ArrayOfString>

My code with HTTPUtils:

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim PostUrl As String 
   PostUrl = "http://142.114.2.131/Sitios/HolaMundo/HolaMundo.asmx?op=Lista"
   Dim parser As SaxParser 
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 Lista As ListView
   Dim TxtBox1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   HttpUtils.CallbackActivity = "Main"   
End Sub

Sub Activity_Resume
   If HttpUtils.Complete = True Then JobDone(HttpUtils.Job)
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub TxtBox1_EnterPressed
   Dim parametro As String
   parametro = TxtBox1.Text 
   LlamarFuncion(parametro)
End Sub

Sub LlamarFuncion(parametro As String)
   ProgressDialogShow("Buscando. . .")
   HttpUtils.PostString("POST Job1", PostUrl, parametro)
   HttpUtils.CallbackJobDoneSub = "JobDone"
   HttpUtils.CallbackUrlDoneSub = "UrlDone"
End Sub

Sub UrlDone(Url As String)
   Log(Url & " done")
   ProgressDialogHide
End Sub

Sub JobDone (Job As String)
   Select Job
      Case "POST Job1"
      Dim cadena As InputStream 
         If HttpUtils.IsSuccess(PostUrl) Then
         cadena =  HttpUtils.GetInputStream(PostUrl)
            'Log(HttpUtils.GetString("http://www.b4x.com/print.php"))
            parser.Parse(cadena, "Parser")
            cadena.Close 
         End If
   End Select
   HttpUtils.Complete = False 'Turn off the complete flag so we won't handle it again if the activity is resumed.
End Sub

Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
      
   For i = 0 To parser.Parents.Size 
   Lista.AddSingleLine(parser.Parents.Get(i))
   Next

End Sub

The error:

Your server expects a soap message. This is an xml message. You are sending it the wrong value. You should send an XML string as the parameter (the exact format depends on your server setttings).

I guess that here not having examples for doing that... right?

In Eclipse I tried usage ksoap2 library, but the library fail when catch the response...
and here (now) I have problems to send parameter (when I thought it would be more easy using B4A )

Maybe Android don't loves me xD

Well, i'll try thanks Erel
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…