Dim j As HttpJob
j.Initialize("", Me)
j.Download("https://www.google.com")
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Log(j.GetString)
End If
j.Release
It works fine.
Now I have a Code Module named functions. I want to add a function like the following:
B4X:
Sub GetLatestNews(activity As Object) As Boolean 'ignore
Dim job As HttpJob
job.Initialize("",activity)
job.Download("http://MyUrl/api/news")
Wait For (job) JobDone(job As HttpJob)
If job.Success Then
'Some Code here
End If
job.Release
End Sub
But B4A IDE says "Static Code Modules can't handle events"
I tried using Standard Class Module instead of Code Module:
B4X:
Sub Class_Globals
Dim JustALabel As Label 'Ignore Just 4 getting rid of the error : Activity context is required
End Sub
Public Sub Initialize
End Sub
Sub GetLatestNews() As String 'ignore
Dim job As HttpJob
job.Initialize("",Me)
job.Download("MyUrl/api/news")
Wait For (job) JobDone(job As HttpJob)
If job.Success Then
Dim res As String
res = job.GetString
job.Release
Return res
Else
job.Release
Return "Error Here"
End If
End Sub
Now I got a new error . "ResumableSub return type must be resumablesub or none"
But I need my method returns what happened and what caused the error
Remember that that a call to Sleep or Wait For in a resumable sub causes the code flow to return to the parent. Example: Sub Button1_Click Sum(1, 2) Log("after sum") End Sub Sub Sum(a As Int, b As Int) Sleep(100) 'this will cause the code flow to return to the parent Log(a + b) End Sub...
www.b4x.com
In the class
B4X:
Sub GetLatestNews As ResumableSub
...
Return Res
End Sub
Elsewhere
B4X:
Dim MyClass1 as MyClass
MyClass1.Initialize
...
Wait For (MyClass1.GetlatestNews) Complete (result as String)
...
End Sub
Thank you agraham , How can I return the reason behind the failure?
Do you think I should return string or boolean?
Do I need to to use try catch inside the method to get the reason?
1) If you only want to return success or failure return a Boolean.
2) If want to return some data on success you could return a Boolean and place the data in a global String variable, or return the data in a String and use a special value to indicate failure. e.g. "<*Failure reason*>".
3) Also you could return a String array with one element as a success indicator and other elements containing other data.