#Region Service Attributes
#StartAtBoot: False
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim hc As HttpClient
Dim my_buffer As OutputStream
End Sub
Sub Service_Create
End Sub
Sub getElevationData(url As String, taskID As Int)
hc.Initialize("hc")
Dim req As HttpRequest
req.InitializeGet(url)
hc.Execute(req,taskID)
End Sub
Sub hc_ResponseSuccess (ResponseData As HttpResponse, TaskId As Int)
my_buffer.InitializeToBytesArray(5000)
'Do Something with the data that is returned.
ResponseData.GetAsynchronously("ServerResponse",my_buffer, True, TaskId)
End Sub
Sub hc_ResponseError (ResponseData As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
Log("error: " & ResponseData & " " & StatusCode)
If ResponseData <> Null Then
Log(ResponseData.GetString("UTF8"))
ResponseData.Release
End If
End Sub
Sub ServerResponse_StreamFinish (Success As Boolean, TaskId As Int)
If Success Then
Dim return_buffer () As Byte
return_buffer = my_buffer.ToBytesArray
Dim returnData As String = BytesToString(return_buffer, 0, return_buffer.Length, "UTF8")
'Now we must do something with the data that has come back from the api server
DecodeJsonResponse(returnData)
'If it is the last Task then close the service. The value here MUST be the same as the last value passed by the activity
If TaskId = 2 Then
StopService("")
End If
End If
End Sub
Sub DecodeJsonResponse(jsonResponse As String)
Dim JSON As JSONParser
Dim Map1 As Map
JSON.Initialize(jsonResponse)
Map1 = JSON.NextObject
Dim m As Map 'helper map for navigating
Dim ResultItems As List
ResultItems = Map1.Get("results")
For i = 0 To ResultItems.Size - 1
m = ResultItems.Get(i)
Next
'Now show the results. These could be assigned to global variable, saved in ini file etc etc
Log("Elevation = " & m.Get("elevation"))
Log("Resolution = " & m.Get("resolution"))
End Sub
Sub Service_Start (StartingIntent As Intent)
End Sub
Sub Service_Destroy
End Sub