Android Question Calling a procedure from another activity module

Lakhtin_V

Active Member
Licensed User
Longtime User
I need to turn to the procedure for processing data on a web server. The manual says that to call a procedure, it is enough to specify the name of the module and the procedure with parameters
upload_2018-11-19_19-26-5.png

upload_2018-11-19_19-15-2.png


In the module 'Main' I have the procedure

upload_2018-11-19_19-13-45.png

. . . . . . . . . . . . .
upload_2018-11-19_19-28-25.png


In another module, I try to call the 'ExecuteRemoteQuery' procedure, but I get an error

upload_2018-11-19_19-32-18.png

Sorry for using screenshots, I thought it would be clearer
 

jimmyF

Active Member
Licensed User
Longtime User
I believe you must use CallSub when calling a routine in an Activity.
You are trying to call a procedure in Main which is an activity.
The procedures you mentioned pertain to code modules only.
 
Upvote 0

Lakhtin_V

Active Member
Licensed User
Longtime User
Thanks DonManfred, for the advice I tried to use CallSubDelayed
I have a problem how to create a resumable procedure 'ExecuteRemoteQuery' in 'Main' module, that returns the result of downloading from the server. Look at the picture
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Thanks DonManfred, for the advice I tried to use CallSubDelayed
I have a problem how to create a resumable procedure 'ExecuteRemoteQuery' in 'Main' module, that returns the result of downloading from the server. Look at the picture

I would suggest you move your ExecuteRemoteQuery sub to a Code Module because I don't think what you are trying to do will work the way you're trying to do it.

Try this:

B4X:
'In a Code Module (eg: "Utils")
Sub ExecuteRemoteQuery(Query as String, JobName as String) as ResumableSub
    ....
    Return nInternet
End Sub

'In your calling module
Wait For (Utils.ExecuteRemoteQuery(strQuery, Main.ALL_WORK)) Complete (otvet as Int)
'do something with otvet

- Colin.
 
Upvote 0

Lakhtin_V

Active Member
Licensed User
Longtime User
I believe you must use CallSub when calling a routine in an Activity.
You are trying to call a procedure in Main which is an activity.
The procedures you mentioned pertain to code modules only.

'In a Code Module (eg: "Main")
Sub ExecuteRemoteQuery(Query as String, JobName as String) as ResumableSub
....
Return nInternet
End Sub

'In my calling module, string Wat For .... causes an error before compiling when writing.
upload_2018-11-21_18-35-19.png
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
'In a Code Module (eg: "Main")
1. Main is your mainactivity. It is NOT a Codemodule
2. Code modules can not raise Events (can not be used in wait for).
3. The error suggests that you do not have such a method in your main. Maybe private/not public?
 
Upvote 0

Lakhtin_V

Active Member
Licensed User
Longtime User
In my other Activity module, I tried two methods
1 Wait For(Main.ExecuteRemoteQuery(strQuery, "ALL_WORK")) Complete (Result As Int) does not work immediately causes a mistake Unknown member: executeremotequery.
2 CallSubDelaed3 method in Main module works only at the beginning.
B4X:
Public Sub ExecuteRemoteQuery(Query As String, JobName As String)As ResumableSub
    Dim str As String, param As String
'    Log("CONECT to WEB server JOB  " & JobName & "  name " & MyModule)
    Try
        Dim job As HttpJob
        job.Initialize(JobName, Me)
'        job.Initialize(JobName, MyModule)
        job.PostString("http://brovarnik.kz/guru.php", Query)
Subsequent lines do not work
B4X:
        Wait For(job) JobDone(job As HttpJob)
        nInternet=-1
'        ProgressDialogHide
        If job.Success Then
            Dim res As String
            res = job.GetString
            Log("WORKING: " & job.JobName)
            Dim parser As JSONParser
            parser.Initialize(res)
            Select job.JobName
                Case "ALL_WORK"
                    Dim lstWork As List
                    lstWork.Initialize
                    Dim dat As List
                    dat = parser.NextArray 'returns a list with map
.....
 
Upvote 0

Lakhtin_V

Active Member
Licensed User
Longtime User
If the Sub ExecuteRemoteQuery procedure is in the same activation module where it is called, I can get a successful result. But it is not convenient for me to repeat such a large procedure in each activation module. I want to write it in one module and use it from different modules
 
Upvote 0

Lakhtin_V

Active Member
Licensed User
Longtime User
I have never used class type modules. It can be shown inside my sample application in my WEBserver.zip file how to do it.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
2. Code modules can not raise Events (can not be used in wait for).

Yep - sorry. Better to use a class.

@Lakhtin_V - creating & using a class is relatively easy. Move the code that you want to access from multiple activities to the class, initialize the class in your Starter service, then access it from anywhere.

Erel has written a pretty comprehensive tutorial on classes here -> Classes tutorial

Below is a quick & dirty example of how you might implement a class in your case.

B4X:
'================================================
'Class module (eg: "clsDBUtils")
Sub Class_Globals
    ' Class member variable declarations
    Private m_Something as Int
End Sub

'Initializes the class. You can add parameters to this method if needed.
Public Sub Initialize(initVal as Int) 'Note - initVal is not required. It's just to show you how to use parameters in your class initialization
    m_Something = initVal
End Sub

'Getter & Setter for m_Something member variable
Public Sub getSomething() as Int
    Return m_Something
End Sub

Public Sub setSomething(val as Int)
    m_Something = val
End Sub

Public Sub ExecuteRemoteQuery(Query As String, JobName As String)As ResumableSub
....
    Return nInternet
End Sub
'======================================================

'=====================================================
'Starter service
Sub Process_Globals
    Public cDBUtils as clsDBUtils
End Sub

Sub Service_Create
    cDBUtils.Initialize(999)
End Sub
'=======================================================

'======================================================
'Activity
 Wait For(Starter.cDBUtils.ExecuteRemoteQuery(strQuery, "ALL_WORK")) Complete (Result As Int)

- Colin.
 
Upvote 0

Lakhtin_V

Active Member
Licensed User
Longtime User
Yep - sorry. Better to use a class.

@Lakhtin_V - creating & using a class is relatively easy. Move the code that you want to access from multiple activities to the class, initialize the class in your Starter service, then access it from anywhere.

Erel has written a pretty comprehensive tutorial on classes here -> Classes tutorial

Below is a quick & dirty example of how you might implement a class in your case.

B4X:
'================================================
'Class module (eg: "clsDBUtils")
Sub Class_Globals
    ' Class member variable declarations
    Private m_Something as Int
End Sub

'Initializes the class. You can add parameters to this method if needed.
Public Sub Initialize(initVal as Int) 'Note - initVal is not required. It's just to show you how to use parameters in your class initialization
    m_Something = initVal
End Sub

'Getter & Setter for m_Something member variable
Public Sub getSomething() as Int
    Return m_Something
End Sub

Public Sub setSomething(val as Int)
    m_Something = val
End Sub

Public Sub ExecuteRemoteQuery(Query As String, JobName As String)As ResumableSub
....
    Return nInternet
End Sub
'======================================================

'=====================================================
'Starter service
Sub Process_Globals
    Public cDBUtils as clsDBUtils
End Sub

Sub Service_Create
    cDBUtils.Initialize(999)
End Sub
'=======================================================

'======================================================
'Activity
 Wait For(Starter.cDBUtils.ExecuteRemoteQuery(strQuery, "ALL_WORK")) Complete (Result As Int)

- Colin.

I updated my test project in zip file, by your advice.
But I immediately had errors in the class module.
Maybe you check my test project. It's small and simple.
I have three activatity in one Activity click the button we launch the request. In another activity, you should see how many rows are processed.
 

Attachments

  • WEBserver V2 rev.zip
    85.6 KB · Views: 177
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
I updated my test project in zip file, by your advice.
But I immediately had errors in the class module.
Maybe you check my test project. It's small and simple.
I have three activatity in one Activity click the button we launch the request. In another activity, you should see how many rows are processed.

You seem to have tried to combine a Service & a Class into the same module. You create a new Class module by clicking Project->Add New Module->Class Module->Standard Class in the B4A IDE.

- Colin.
 
Upvote 0
Top