Android Question OkHttpUtils2 - How to run from code module

rzv25

Member
Licensed User
Longtime User
Hello all,

This thread is related to this question that I had.
I have a project where my functionalities are split in code modules. In the gps related functionality code module, I want to make a request to Google Api services to get the address based on latitude and longitude.

With HTTP library I could do this using a HttpRequest object and then in its ResponseSuccess event get the result string with Response.GetString, where Response is a parameter of the event of type HttpResponse.

I understood that is recommended to switch to OkHTTP library but the OkHttpResponse object does not have the GetString method anymore. So the switch is not so simple as replacing Http with OkHttp in the code.
The suggested method of doing this request with OkHttp is to use a HttpJob. The problem is that when initializing the HttpJob, you need to pass the targetModule object. And because my code is in a code module, I cannot simply pass a reference like 'Me'.

So, is there a way to have this feature running with OkHttp and still be part of a code module ?

Thank you
 

DonManfred

Expert
Licensed User
Longtime User
Code module can NEVER use any Events.
You can not use Code Modules for httpjobs.
Use the Starter service or use Classes.
 
Upvote 0

BillMeyer

Well-Known Member
Licensed User
Longtime User
Here's some help. Modify as you need.

Libraries required (over and above the normal stuff)

1. Geocoder
2. LocationAPI
3. GPS

This code may not work as I have not tested it in a "stand alone" application - but use it and modify it as you like.

Also remember, that the address could NOT BE accurate - this depends on your device.

Initial code comes from this forum - modified slightly for my use.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim GPS1 As GPS
    Dim Place As Location
    Dim Geocoder1 As Geocoder
End Sub


Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private DESCInput As EditText
    Private DES_CancelButton As Button
    Private DES_KeepButton As Button
    Private DES_LocationDescription As EditText
   
    Dim ResultsList As ListView
    Dim su As StringFunctions
    Dim GeoLat, GeoLon As Double

    Dim Lon As String
    Dim Lat As String
    Dim Speed As String
   
    Dim OnceOff As Int = 1
 
    Dim SL As Boolean = False
    Private ShowLocation As ToggleButton
   
End Sub


Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("GeocodeDecipher")
    Place.Initialize
    ResultsList.Initialize("")
   
    If FirstTime Then
        GPS1.Initialize("GPS")
        Geocoder1.Initialize("Geocoder1")
    End If
   
    DESCInput.SingleLine = False
    DES_LocationDescription.SingleLine = False

End Sub

Sub Activity_Resume
    If GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    Else
        GPS1.Start(0, 0) 'Listen to GPS with no filters.
    End If
       
End Sub

Sub Activity_Pause (UserClosed As Boolean)
     GPS1.Stop
End Sub

Sub GPS_LocationChanged (Location1 As Location)
    ' Lat =  Place.ConvertToMinutes(Location1.Latitude)
    ' Lon =  Place.ConvertToMinutes(Location1.Longitude)
    Lat =  (Location1.Latitude)
    Lon =  (Location1.Longitude)
    GeoLat = Location1.Latitude
    GeoLon = Location1.Longitude
    ' Log("Lat: "&Lat&" Lon: "&Lon)
    Speed = Location1.Speed
    Location
End Sub

Sub Location

      Dim MapIntent As Intent
      Dim myPosition As String
      Dim MaxResults As Int = 5
     
      ' myPosition = "geo:0,0?q=" & "40.7142, -74.0064"
   
      Log("Latitude: "&Lat)
      Log("Longitude: "&Lon)
     
      myPosition = "geo:"&GeoLat&","&GeoLon&"?q=" & Lat &","& Lon
      MapIntent.Initialize(MapIntent.ACTION_VIEW, myPosition)
      Geocoder1.GetFromLocation(GeoLat, GeoLon, MaxResults, Null)
   
End Sub

Sub Geocoder1_GeocodeDone(Results() As Address, Tag As Object)
   
    Dim AddressList As String
    If Results.Length > 0 Then
        Dim Address1 As Address
        Dim i As Int
        For i=0 To Results.Length -1
            Address1=Results(i)
            Log("Address1.AddressLines.Size="&Address1.AddressLines.Size)
            If Address1.AddressLines.Size>0 Then
                ResultsList.AddTwoLines(Address1.AddressLines.Get(0), Address1.AddressLines.Get(0))
                AddressList = Address1.AddressLines.Get(0)&CRLF&Address1.Locality&CRLF&Address1.PostalCode&CRLF&Address1.CountryName
                ' Log(Address1.SubLocality&" - "&Address1.Thoroughfare)
                ' Msgbox("Closest Known Address is:"&CRLF&AddressList,"DEBUG")
                If OnceOff = 1 Then
                   DES_LocationDescription.Text = AddressList
                   DES_LocationDescription.Invalidate
                   GPS1.Stop
                   OnceOff = OnceOff + 1
                End If
            End If
        Next
    Else
        Msgbox("GetFromLocation", "No Address matched the Latitude and Longitude")
    End If
End Sub
 
Upvote 0
Top