Android Question Call JobDone Method - Return Value in a Class

Discussion in 'Android Questions' started by MarcoRome, Jul 9, 2015.

  1. MarcoRome

    MarcoRome Expert Licensed User

    Hi all.

    i have this class:
    Code:
    '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

    Code:
    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
     
  2. DonManfred

    DonManfred Expert Licensed User

    Start the job and then raise an event in jobdone.
    Make your code working ASync.
     
  3. MarcoRome

    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.

    Code:
    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
     
  4. MarcoRome

    MarcoRome Expert Licensed User

    Example:

    Code:
    '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:

    Code:
    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
    Code:
    if aa.xx = "OK" then....
    runs before that job finish ( asynchronous )
     
    Last edited: Jul 9, 2015
  5. sonicmayne

    sonicmayne Member Licensed User

    This will do the trick:

    Code:
    '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:
    Code:
    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
     
    Jorge M A and MarcoRome like this.
  6. MarcoRome

    MarcoRome Expert Licensed User

    Soon i will try. Thank you for now sonicmayne
     
  7. MarcoRome

    MarcoRome Expert Licensed User

    Work with out problem.
    Thank you very much @sonicmayne
     
    sonicmayne likes this.
  8. walterf25

    walterf25 Well-Known Member Licensed User

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

    MarcoRome Expert Licensed User

    Hi walterf25. Right and solution @sonicmayne work great ;)
     
  10. aidymp

    aidymp Active Member Licensed User

    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 :
    Code:
    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

    Code:
    CallSub2(Main,EN,res)
    I have tried

    Code:
    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
     
  11. DonManfred

    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: Oct 29, 2015
    sonicmayne and aidymp like this.
  12. aidymp

    aidymp Active Member Licensed User

    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
    Is there a simpler way?

    My Class PHPResponse

    Code:
    '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

    Code:
    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

    Code:
    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
     
  13. MaFu

    MaFu Well-Known Member Licensed User

    Please note if you use obfuscation then the names of the subs which are called with CallSub() or CallSubDelayed() must contain a underscore!
     
    aidymp likes this.
  14. DonManfred

    DonManfred Expert Licensed User

    shouldn´t it not be
    Code:
    CallSub2(MN,EN,res)
     
    sonicmayne and aidymp like this.
  15. aidymp

    aidymp Active Member Licensed User

    NOW I know this!!

    Thanks

    Aidy
     
  16. aidymp

    aidymp Active Member Licensed User

    YES! but I was trying various things and forgot to replace that!

    Thanks

    Aidy
     
Loading...