Android Question Call JobDone Method - Return Value in a Class

MarcoRome

Expert
Licensed User
Hi all.

i have this class:
B4X:
'Class module
Sub Class_Globals
    Dim valore As String

End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize ( email As String ) as String
    Dim dominio() As String
    dominio = Regex.Split("@", email)
    Dim job1 As HttpJob
    job1.Initialize("MyJob", Me)
    job1.Download("https://xxxx.com/" & dominio(1))
    return valore
End Sub

Sub JobDone(job As HttpJob)
    ProgressDialogHide
    If job.Success Then
        Dim res As String
        res = job.GetString
        'Log("Back from Job:" & Job.JobName )
        Log("Response from server: " & res)
        
        Dim parser As JSONParser
        parser.Initialize(res)

        Dim m As Map
        m = parser.NextObject
        Log(m.Get("status"))
   
        valore = m.Get("status") '<------- THIS IS RETURN VALUE "OK" and work
    Else
        ToastMessageShow("Error: " & job.ErrorMessage, True)
    End If
    job.Release
End Sub

And this is activity

B4X:
    Dim aa As checkemail
    Dim vediamo As String
  if  aa.Initialize("info@devil-app.com") = "OK" then....

Now this is wrong
the valore = is "" because dont wait Jobdone - asyn - etc. ... What do i do to return the value ??
Any idea ??
Thank you
Marco
 

DonManfred

Expert
Licensed User
Start the job and then raise an event in jobdone.
Make your code working ASync.
 
Upvote 0

MarcoRome

Expert
Licensed User
Thank you DonManfred for your response but dont understand.
I try to explain better.
I would like to run the class await the return value

As you see in Initialize run job1 but being asynchronous, the value valore is "" because dont wait job.

B4X:
Public Sub Initialize ( email As String ) as String
    Dim dominio() As String
    dominio = Regex.Split("@", email)
    Dim job1 As HttpJob
    job1.Initialize("MyJob", Me)
    job1.Download("https://xxxx.com/" & dominio(1))
    return valore
End Sub

thank you
Marco
 
Upvote 0

MarcoRome

Expert
Licensed User
Example:

B4X:
'Class module
Sub Class_Globals
    Dim valore As String

End Sub


'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize ( email As String )
    Dim dominio() As String
    dominio = Regex.Split("@", email)
    Dim job1 As HttpJob
    job1.Initialize("MyJob", Me)
    job1.Download("https://xxxx.com/" & dominio(1))
End Sub

Sub xx As String
     return valore
End Sub

Sub JobDone(job As HttpJob)
    ProgressDialogHide
    If job.Success Then
        Dim res As String
        res = job.GetString
        'Log("Back from Job:" & Job.JobName )
        Log("Response from server: " & res)
      
        Dim parser As JSONParser
        parser.Initialize(res)

        Dim m As Map
        m = parser.NextObject
        Log(m.Get("status"))
 
        valore = m.Get("status") '<------- THIS IS RETURN VALUE "OK" and work
        xx ' <------ CALL THIS AFTER THAT valore is ok
    Else
        ToastMessageShow("Error: " & job.ErrorMessage, True)
    End If
    job.Release
End Sub

But in Activity if i have this code:

B4X:
Dim aa As checkemail
    Dim vediamo As String
aa.Initialize("info@devil-app.com")
if aa.xx = "OK" then....

i have aa.xx ever "" because
B4X:
if aa.xx = "OK" then....
runs before that job finish ( asynchronous )
 
Last edited:
Upvote 0

sonicmayne

Member
Licensed User
This will do the trick:

B4X:
'Class module
Sub Class_Globals
    Dim valore As String
    Private MN as Object
    Private EN As String
End Sub


'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize ( email As String, Module As Object, EventName As String )
    Dim dominio() As String
    dominio = Regex.Split("@", email)
    Dim job1 As HttpJob
    job1.Initialize("MyJob", Me)
    job1.Download("https://xxxx.com/" & dominio(1))
    MN = Module
    EN = EventName
End Sub

Sub JobDone(job As HttpJob)
    ProgressDialogHide
    If job.Success Then
        Dim res As String
        res = job.GetString
        'Log("Back from Job:" & Job.JobName )
        Log("Response from server: " & res)
     
        Dim parser As JSONParser
        parser.Initialize(res)

        Dim m As Map
        m = parser.NextObject
        Log(m.Get("status"))
        valore = m.Get("status") '<------- THIS IS RETURN VALUE "OK" and work
        CallSub2(MN,EN,valore)
    Else
        ToastMessageShow("Error: " & job.ErrorMessage, True)
    End If
    job.Release
End Sub

In your main class you'd put:
B4X:
Dim aa As checkemail
    Dim vediamo As String
aa.Initialize("info@devil-app.com",Me,"checkemail")


Sub checkemail(Valore As String)
'Do whatever
End Sub
 
Upvote 0

MarcoRome

Expert
Licensed User
This will do the trick:

B4X:
'Class module
Sub Class_Globals
    Dim valore As String
    Private MN as Object
    Private EN As String
End Sub


'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize ( email As String, Module As Object, EventName As String )
    Dim dominio() As String
    dominio = Regex.Split("@", email)
    Dim job1 As HttpJob
    job1.Initialize("MyJob", Me)
    job1.Download("https://xxxx.com/" & dominio(1))
    MN = Module
    EN = EventName
End Sub

Sub JobDone(job As HttpJob)
    ProgressDialogHide
    If job.Success Then
        Dim res As String
        res = job.GetString
        'Log("Back from Job:" & Job.JobName )
        Log("Response from server: " & res)
    
        Dim parser As JSONParser
        parser.Initialize(res)

        Dim m As Map
        m = parser.NextObject
        Log(m.Get("status"))
        valore = m.Get("status") '<------- THIS IS RETURN VALUE "OK" and work
        CallSub2(MN,EN,valore)
    Else
        ToastMessageShow("Error: " & job.ErrorMessage, True)
    End If
    job.Release
End Sub

In your main class you'd put:
B4X:
Dim aa As checkemail
    Dim vediamo As String
aa.Initialize("info@devil-app.com",Me,"checkemail")


Sub checkemail(Valore As String)
'Do whatever
End Sub

Soon i will try. Thank you for now sonicmayne
 
Upvote 0

MarcoRome

Expert
Licensed User
This will do the trick:

B4X:
'Class module
Sub Class_Globals
    Dim valore As String
    Private MN as Object
    Private EN As String
End Sub


'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize ( email As String, Module As Object, EventName As String )
    Dim dominio() As String
    dominio = Regex.Split("@", email)
    Dim job1 As HttpJob
    job1.Initialize("MyJob", Me)
    job1.Download("https://xxxx.com/" & dominio(1))
    MN = Module
    EN = EventName
End Sub

Sub JobDone(job As HttpJob)
    ProgressDialogHide
    If job.Success Then
        Dim res As String
        res = job.GetString
        'Log("Back from Job:" & Job.JobName )
        Log("Response from server: " & res)
    
        Dim parser As JSONParser
        parser.Initialize(res)

        Dim m As Map
        m = parser.NextObject
        Log(m.Get("status"))
        valore = m.Get("status") '<------- THIS IS RETURN VALUE "OK" and work
        CallSub2(MN,EN,valore)
    Else
        ToastMessageShow("Error: " & job.ErrorMessage, True)
    End If
    job.Release
End Sub

In your main class you'd put:
B4X:
Dim aa As checkemail
    Dim vediamo As String
aa.Initialize("info@devil-app.com",Me,"checkemail")


Sub checkemail(Valore As String)
'Do whatever
End Sub

Work with out problem.
Thank you very much @sonicmayne
 
Upvote 0

walterf25

Expert
Licensed User
Hi all.

i have this class:
B4X:
'Class module
Sub Class_Globals
    Dim valore As String

End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize ( email As String ) as String
    Dim dominio() As String
    dominio = Regex.Split("@", email)
    Dim job1 As HttpJob
    job1.Initialize("MyJob", Me)
    job1.Download("https://xxxx.com/" & dominio(1))
    return valore
End Sub

Sub JobDone(job As HttpJob)
    ProgressDialogHide
    If job.Success Then
        Dim res As String
        res = job.GetString
        'Log("Back from Job:" & Job.JobName )
        Log("Response from server: " & res)
       
        Dim parser As JSONParser
        parser.Initialize(res)

        Dim m As Map
        m = parser.NextObject
        Log(m.Get("status"))
  
        valore = m.Get("status") '<------- THIS IS RETURN VALUE "OK" and work
    Else
        ToastMessageShow("Error: " & job.ErrorMessage, True)
    End If
    job.Release
End Sub

And this is activity

B4X:
    Dim aa As checkemail
    Dim vediamo As String
  if  aa.Initialize("info@devil-app.com") = "OK" then....

Now this is wrong
the valore = is "" because dont wait Jobdone - asyn - etc. ... What do i do to return the value ??
Any idea ??
Thank you
Marco

You need to check the value of "Valore" inside the Sub JobDone, not right after you call job1.Download("https://xxxx.com/" & dominio(1))
 
Upvote 0

aidymp

Active Member
Licensed User
This will do the trick:

B4X:
'Class module
Sub Class_Globals
    Dim valore As String
    Private MN as Object
    Private EN As String
End Sub


'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize ( email As String, Module As Object, EventName As String )
    Dim dominio() As String
    dominio = Regex.Split("@", email)
    Dim job1 As HttpJob
    job1.Initialize("MyJob", Me)
    job1.Download("https://xxxx.com/" & dominio(1))
    MN = Module
    EN = EventName
End Sub

Sub JobDone(job As HttpJob)
    ProgressDialogHide
    If job.Success Then
        Dim res As String
        res = job.GetString
        'Log("Back from Job:" & Job.JobName )
        Log("Response from server: " & res)
    
        Dim parser As JSONParser
        parser.Initialize(res)

        Dim m As Map
        m = parser.NextObject
        Log(m.Get("status"))
        valore = m.Get("status") '<------- THIS IS RETURN VALUE "OK" and work
        CallSub2(MN,EN,valore)
    Else
        ToastMessageShow("Error: " & job.ErrorMessage, True)
    End If
    job.Release
End Sub

In your main class you'd put:
B4X:
Dim aa As checkemail
    Dim vediamo As String
aa.Initialize("info@devil-app.com",Me,"checkemail")


Sub checkemail(Valore As String)
'Do whatever
End Sub

Hi I am not sure if its me but I cant get this to work! I have a class called PHPResponce I call it from the main module like so :
B4X:
Dim aaa,bbb,ccc As PHPResponce
aaa.Initialize("SM2",Me,"sm2dl")
bbb.Initialize("SM2L",Me,"sm2ldl")
ccc.Initialize("SM2LA",Me,"sm2ladl")

    End Sub



Sub sm2dl(Result As String)
Log("NewClass - SM2 Downloads ="&Result)
SM2=Result
End Sub   

Sub sm2ldl(Result As String)
Log("NewClass - SM2L Downloads ="&Result)
SM2L=Result
End Sub   

Sub sm2ladl(Result As String)
Log("NewClass - SM2LA Downloads ="&Result)
SM2LA=Result
End Sub

In the Class I log the results so I can see its actually working and it is!

It all seems to fail here in the PHPResponce class

B4X:
CallSub2(Main,EN,res)

I have tried

B4X:
CallSub2(Main,"sm2la",res)
and the sub is called!?!? but using EN (the variable EVENT NAME) it will not fire?

Where have I gone wrong?

Thanks

Aidy
 
Upvote 0

DonManfred

Expert
Licensed User
Please post the complete class code. It would help us much here.

The JobDone is missing in class (maybe it is but due to missing code i can not say anything more about), the code where you dim and start a job is missing too.

As of yet it looks for me that you are using the class in a wrong way. Or maybe you do it more complicated than it should be. ;-)

You class code can answer this ;-)
 
Last edited:
Upvote 0

aidymp

Active Member
Licensed User
Please post the complete class code. It would help us much here.

The JobDone is missing in class (maybe it is but due to missing code i can not say anything more about), the code where you dim and start a job is missing too.

As of yet it looks for me that you are using the class in a wrong way. Or maybe you do it more complicated than it should be. ;-)

You class code can answer this ;-)

HI, thanks for the reply I did not post the code, as I am using the code supplied by @sonicmayne I have actually solved (After 4 hours! ;( ) this it was due to Obfuscation!!

So it is working but you say
maybe you do it more complicated than it should be. ;-)
Is there a simpler way?

My Class PHPResponse

B4X:
'Class module PHPResponse
Sub Class_Globals
    Dim result As String
    Private MN As Object
    Private EN As String
End Sub


'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize (bld As String, Module As Object, EventName As String )
  Dim Login As HttpJob
    Login.Initialize("GetDLCount", Me)   
    Login.Download2("http://private.php",Array As String("action","GetDLCount","build", bld))
    Login.Release
    MN = Module
    EN = EventName
End Sub

Sub JobDone(job As HttpJob)
      If job.Success Then
        Dim res As String
        res = job.GetString
        'Log("Back from Job:" & Job.JobName )
        'Log("Response from server: " & res)
        'Log("MN="&MN)
        'Log("EN="&EN)
' This block was what I tried as CallSub2(Main,EN,res) did not work!
' I noticed that with the addition of this code, CallSub2(Main,EN,res) worked!?!?! so it was called twice!
' proving it did actually work! I renamed the subs sm2_dl and now it works!         
'        Select  EN 
'            Case "sm2ldl"
'                CallSub2(Main,"sm2ldl",res)
'            Case "sm2ladl"   
'                CallSub2(Main,"sm2ladl",res)
'            Case "sm2dl"   
'                CallSub2(Main,"sm2dl",res)
'        End Select
         CallSub2(Main,EN,res)
    Else
        ToastMessageShow("Error: " & job.ErrorMessage, True)
    End If
    job.Release
End Sub

This code calls the sub from my main module

B4X:
Dim aaa,bbb,ccc As PHPResponse
aaa.Initialize("SM2","main","sm2_dl")

And these subs in the main module are fired when the result is received

B4X:
Sub sm2_dl(Result As String)
Log("NewClass - SM2 Downloads ="&Result)
SM2=Result
End Sub   

Sub sm2l_dl(Result As String)
Log("NewClass - SM2L Downloads ="&Result)
SM2L=Result
End Sub   

Sub sm2la_dl(Result As String)
Log("NewClass - SM2LA Downloads ="&Result)
SM2LA=Result
End Sub

It is all working correctly, But you say maybe I am using it wrong or it could be optimized? Any tips would be great thanks!

All the code does is calls my php the php looks in a database and returns the result in this case a download counter (for 3 items)
obviously Its not ideal as the names are hard coded, what would I do to expand on this? if say I wanted 10 items?


Aidy
 
Upvote 0
Top