Calling webservice problem

mrjaw

Active Member
Licensed User
Longtime User
Hi!
I am trying to do a module to wrap everything for callilng webservices

this is my module
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
        '
     Dim url As String: url="http://192.168.52.131/ssl/server.php?id="
     Dim hc As HttpClient
     Dim IdCall As Int   : Idcall=101 'ID inicial de las llamadas que se haran a los WS
     hc.Initialize("hc")
End Sub

   
      
Sub ExecuteWs(ind as Int)
   Dim req As HttpRequest
   Dim query As String
   Dim taskId As Int :taskId=IdCall+2
   query="2"
    req.InitializePost2(url, query.GetBytes("UTF8"))
    hc.Execute(req, taskId)
End Sub

Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, taskId As Int)
    Log("Error: " & Reason & ", StatusCode: " & StatusCode)
    If Response <> Null Then
        Log(Response.GetString("UTF8"))
        Response.Release
    End If
    'ProgressDialogHide
End Sub

Sub hc_ResponseSuccess (Response As HttpResponse, taskId As Int)
    Dim res As String
    res = Response.GetString("UTF8")
    Response.Release
   Return res
End Sub

I wanna use something like that st1=ExecuteWs(1) but like I saw it cant be because the method that return a value is hc_ResponseSuccess so my question is

How can I return the value from ExecuteWS ?

Thks
 

mrjaw

Active Member
Licensed User
Longtime User
I saw this module but I dont understand some parts about Job and download.
Maybe this module needs more explanation.
 
Upvote 0

ScarBelly

Member
Licensed User
Longtime User
The documentations worked for me. Here is one example of how I do it.

Sub btnLogIn_Click
If btnLogIn.Text = "Log In" AND PollStatusService.ClientID = 0 AND txtLogIn.Text <> "" Then
phMain.hidekeyboard(Activity)
PostUrl = PostUrlRoot & "?MAC=" & MacAddress & "&User=" & txtLogIn.Text.Trim 'Mac and password
HttpUtils.PostString("RequestLogIn", PostUrl, "")
HttpUtils.CallbackJobDoneSub = "JobDone"
HttpUtils.CallbackActivity = "Main" 'Current activity name.
ProgressDialogShow("Fetching data...")
End Id
End Sub

Sub JobDone (Job As String)
If HttpUtils.IsSuccess(PostUrl) Then
Dim response As String
Select Job
Case "ChkLogIn", "RequestLogIn" response = HttpUtils.GetString(PostUrl)
.....
....
 
Upvote 0

mrjaw

Active Member
Licensed User
Longtime User
I did something like that. My prooblem is because the return value is done for "JobDone" and this is done fired as event.

I can not return the value using this form because I dont know where Jobdone will return the value.

I wanted to use this way to use

lvalue = ExecuteWebService(1,VALUE_SQL)

but with JobDone I cant do this way.

I used httpUtils and I dont have problem but anytime that I need to call a wS I have to do a JonDone or other CallActivity, this is so hard.

Unfortunately, I think there is no other way to call a websrvice just using JobDone.

:sign0013:
 
Upvote 0

Jost aus Soest

Active Member
Licensed User
Longtime User
Idea:
You could set a global variable with the result in hc_ResponseSuccess and in ExecuteWs you're looping till this variable is set. Than it's easy to return this variable...

But:
What shall happen if the whole process takes too long?
Maybe you could also limit the loop to 1 or 2 seconds?
 
Upvote 0

mrjaw

Active Member
Licensed User
Longtime User
Uhmmm...your idea is not so bad!!!

I dont like the idea to run a loop asking for a value but I test this solution anyway. This is very smart!!!!

Thks!!

PD: b4a , doesn't support use SSL with httpClient or httpUtils ?
 
Upvote 0
Top