hc_ResponseSuccess in a code module does not run

isuru

Member
Licensed User
Longtime User
In my app, there's an Activity called Search. In it, there's a search button, here's the code which executes on the button's click event.

B4X:
Sub btnSearch_Click
   Dim selection, search_string As String
   Dim range As CostRange
   
   selection = spnrSearchBy.SelectedItem.Trim
   search_string = txtSearch.Text.Trim
   range.FromCost = txtFrom.Text.Trim
   range.ToCost = txtTo.Text.Trim

   If selection = "Cost" Then
      Common.ChooseQuery(selection, Null, range.FromCost, range.ToCost)
   Else
      Common.ChooseQuery(selection, search_string, Null, Null)
   End If
End Sub 'end of btnSearch_Click

And I have added a code module named Common which contains code regarding connecting to an online MySQL database and retrieving results.

B4X:
Sub Process_Globals
   Dim hc As HttpClient
        Dim SEARCH_RESULTS = 1
End Sub

Sub ChooseQuery (param As String, str As String, fc As String, tc As String)
   ProgressDialogShow2("Please wait", False)
   If param = "Area" Then
      ExecuteRemoteQuery("SELECT T.SID, Name, Cost FROM locations L INNER JOIN service_locations SL ON L.Code = SL.Loc_Code LEFT JOIN taxi_services T ON T.SID =  SL.SID WHERE Location LIKE '%" & str.Trim & "%' AND Active = 1 GROUP BY T.SID ", SEARCH_RESULTS)
   Else If param = "Cost" Then
      ExecuteRemoteQuery("SELECT SID, Name, Cost FROM taxi_services WHERE Cost >= '" & fc & "' AND Cost <= '" & tc & "' AND Active = 1 ", SEARCH_RESULTS)
   Else If param = "Service Name" Then
      ExecuteRemoteQuery("SELECT SID, Name, Cost FROM taxi_services WHERE Name LIKE '%" & str.Trim & "%' AND Active = 1 ", SEARCH_RESULTS)
   Else If param = "Hotline" Then
      ExecuteRemoteQuery("SELECT S.SID, Name, Cost, Phone_No FROM hotlines H INNER JOIN taxi_services S ON H.SID = S.SID WHERE Phone_No LIKE '%" & str.Trim & "%' AND Active = 1 ", SEARCH_RESULTS)
   End If
End Sub

Sub ExecuteRemoteQuery (Query As String, TaskId As Int)
   Dim req As HttpRequest
   req.InitializePost2("http://mysite.com/service.php", Query.GetBytes("UTF8"))
        hc.Initialize("hc")
   hc.Execute(req, TaskId)
End Sub

Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   Dim res As String
   res = Response.GetString("UTF8")
   Log("Response from server: " & res)
   Dim parser As JSONParser
   parser.Initialize(res)
   Select TaskId
      Case SEARCH_RESULTS
         Dim ressulset As List
         ressulset = parser.NextArray
         If ressulset.Size = 0 Then
            StartActivity(NoResults)
         Else
            StartActivity(Results)
         End If
         'ProgressDialogHide
   End Select
   Response.Release
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

The ExecuteRemoteQuery sub gets executed but the hc_ResponseSuccess event does not run. The execution halts at the end of btnSearch_Click sub.

What should I do to correct this?

Thank you :)
 
Last edited:

isuru

Member
Licensed User
Longtime User
Okay I kinda figured out why that error occurs. I guess the hc_ResponseSuccess should be within the same Sub. Placing it in the same sub resolved the issue.

But I'm still interested to know if there's a way to abstract the code regarding HttpClient library (ResponseSuccess, ResponseError etc.) to a separate file.
 
Upvote 0

Penko

Active Member
Licensed User
Longtime User
hc_ResponseSuccess is an event. You can only handle events in Activity modules(+ Class modules which share the life cycle of the calling Activity). You can't handle events in Code Modules. But you can do something like this:

Code module: Handler
B4X:
Sub ResponseSuccess(someArg As String) '
Log("My response is successful")

' do some other stuff here

End Sub


Then, in your Activity 1:
B4X:
Sub hc_ResponseSuccess(someArg as String)
Handler.ResponseSuccess(someArg)
End Sub

Then, in your Activity 2:
B4X:
Sub hc_ResponseSuccess(someArg as String)
Handler.ResponseSuccess(someArg)
End Sub

Then, in your Activity n-1:
B4X:
Sub hc_ResponseSuccess(someArg as String)
Handler.ResponseSuccess(someArg)
End Sub

The same goes for handling ResponseError or any other event. Repetitive logic is always better isolated inside a Code Module and events from different activities should only call the corresponding method. If you need to do something to the calling Activity, you can always pass a reference to it via an argument.
 
Upvote 0
Top