Android Question Calling HTTPJOB from a class

MotoMusher

Active Member
Licensed User
Longtime User
Maybe it's been a long day, but I am trying to call HTTPJOB from a class since this collection of Subs gets called from multiple activities. It works fine from an activity passing "ME" as the target with jobdone in the activity. I get a null object error from the class immediately when I initialize, which makes sense. So, I put my jobdone event in Main, and pass Main as the target object and that gives me null object errors as well.

java.lang.NullPointerException: Attempt to read from field 'anywheresoftware.b4a.BALayout anywheresoftware.b4a.BA.vg' on a null object reference

Any ideas?
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Try putting JobDone() inside the class and have your class raise an event using CallSubDelayed() when it has processed JobDone().

An example:
B4X:
'ExampleClass
#Event: RequestFinished

Sub Class_Globals
    Private cbObj as Object
    Private cbEN as String
End Sub

Sub Initialize(callbackModule as Object, callbackEventName as String)
    cbObj = callbackObj
    cbEN = callbackEventName
End Sub

Sub StartRequest
    Dim hj as HttpJob
    hj.Initialize("hj", Me)
    hj.Download("http://b4x.com/android/forum/threads/calling-httpjob-from-a-class.58672/")
End Sub

Sub JobDone(Job as HttpJob)
    CallSubDelayed(cbObj, cbEN & "_RequestFinished")
    Job.Release
End Sub

Then, in some Activity:
B4X:
Sub StartHere
    Dim ec as ExampleClass
    ec.Initialize(Me, "ec")
    ec.StartRequest
End Sub

Sub ec_RequestFinished
    Log("RequestFinished")
End Sub
 
Upvote 0

FrankDev

Active Member
Licensed User
Longtime User
Try putting JobDone() inside the class and have your class raise an event using CallSubDelayed() when it has processed JobDone().

An example:
B4X:
'ExampleClass
#Event: RequestFinished

Sub Class_Globals
    Private cbObj as Object
    Private cbEN as String
    Private cbURL as String   'new
End Sub

Sub Initialize(callbackModule as Object, callbackEventName as String, URL as String)   'new
    cbObj = callbackObj
    cbEN = callbackEventName
    cbURL = URL 'new
End Sub

Sub StartRequest
    Dim hj as HttpJob
    hj.Initialize("hj", Me)
    hj.Download(cbURL) 'new
End Sub

Sub JobDone(Job as HttpJob)
    dim http_answer as String  'new
    http_answer =Job.GetString 'new
    CallSubDelayed2(cbObj, cbEN & "_RequestFinished",http_answer)  'new
    Job.Release
End Sub

Then, in some Activity:
B4X:
Sub StartHere
    Dim ec as ExampleClass
    ec.Initialize(Me, "ec","http://b4x.com/android/forum/threads/calling-httpjob-from-a-class.58672/") 'new
    ec.StartRequest
End Sub

Sub ec_RequestFinished(http_answer  as String) 'new
    Log("RequestFinished")
End Sub

hi

is this a possibel way to use different url's
what about if the Server do not respond ?

regards Frank
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
You can do this:
B4X:
#Event: RequestError(errorMessage as String)
'Add this new event annotation to the top of the class

Sub StartRequest(url as String)   'pass a URL at run-time
    Dim hj as HttpJob
    hj.Initialize("hj", Me)
    Try
        hj.Download(url)
    Catch   'If the server doesn't respond, hj.Download will throw an Exception
        CallSubDelayed2(cbObj, cbEN & "_RequestError", LastException.Message)
    End Try
End Sub

Sub JobDone(Job as HttpJob)
    If Not(Job.Success) Then    'Also raise the error event here
        CallSubDelayed2(cbObj, cbEN & "_RequestError", Job.ErrorMessage)
    Else
        'Normal operation
    End If
    Job.Release
End Sub
 
Upvote 0

MotoMusher

Active Member
Licensed User
Longtime User
I found the problem. Thanks for the responses.

Issue was that I was calling the class without initializing first from the activity. Class processed multiple subs that are strung together fine, but had no value for ME to pass to httpjob.
 
Upvote 0
Top