Well, I must admit I have never used classes that much. However, now I am rewriting a lot of code transforming them into classes (and perhaps later into B4A-libraries) so the code may be easily (re)used in other similar apps without much ado.
I have gone through most examples and most things seem rather clear. However, I have problems with classes that fetch data using the httputils2-library (or the class). I don't know how to return the data retrieved in the class to the method called in Main. In the class, I think the problem is that the return value is generated in the JobDone-sub (due to the nature of asynchronous data retrieval) and there is no way to pass it back to my method which is called in Main.
Here is some pseudo-code:
let us say I have this method-call in Main:
here is the class mygetdatacall:
How can I get this to work?
to be returned as a result from the class when calling it from Main:
???
I can use callsub or using Process global variables for sharing data within the app but it seems not right when classes are available in B4A and I could retrieve data using a simple method-call. Probably I might be missing something very obvious.
Anyone who can help me to shed some lights? Thanks!
I have gone through most examples and most things seem rather clear. However, I have problems with classes that fetch data using the httputils2-library (or the class). I don't know how to return the data retrieved in the class to the method called in Main. In the class, I think the problem is that the return value is generated in the JobDone-sub (due to the nature of asynchronous data retrieval) and there is no way to pass it back to my method which is called in Main.
Here is some pseudo-code:
let us say I have this method-call in Main:
B4X:
dim mydata as mygetdatacall
mydata.initialize
log(mydata.getXMLreply)
here is the class mygetdatacall:
B4X:
Sub Class_Globals
Dim mysaxparser As SaxParser
Private Myreturndata As String
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
End Sub
Sub getXMLreply As String
Dim JOBgetmydata As HttpJob
Dim myURL As String= "http://192.168.1.80/web/about"
JOBgetmydata.Initialize("JOBgetmydata", Me)
JOBgetmydata.Username="myusername"
JOBgetmydata.Password="mypassword"
JOBgetmydata.Download(myURL)
'return Myreturndata 'does not work - sub is not being used again
End Sub
Sub JobDone (Job As HttpJob)
Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
If Job.Success = True Then
Select Job.JobName
Case "JOBgetmydata"
Try
mysaxparser.Initialize
Dim result_data As InputStream
result_data = Job.GetInputStream
mysaxparser.Parse(result_data, "mysaxparser")
Catch
'do something different
End Try
Return Myreturndata '??? how to return data to the method(sub) getXMLreply
End Select
Else
Log("Error: " & Job.ErrorMessage)
End If
Job.Release
Log("job was released")
End Sub
Sub mysaxparser_EndElement (Uri As String, Name As String, Text As StringBuilder)
If mysaxparser.Parents.IndexOf("about") > - 1 Then
If Name = "service" Then
Myreturndata = Text.ToString
If Myreturndata.Length = 0 Then
Myreturndata = "No data"
End If
End If
End If
End Sub
How can I get this to work?
B4X:
Return Myreturndata
to be returned as a result from the class when calling it from Main:
B4X:
log(mydata.getXMLreply)
???
I can use callsub or using Process global variables for sharing data within the app but it seems not right when classes are available in B4A and I could retrieve data using a simple method-call. Probably I might be missing something very obvious.
Anyone who can help me to shed some lights? Thanks!