Android Question how to use Java Object

gezueb

Active Member
Licensed User
Longtime User
I have to write an App that should communicate with a Navigation Tool which is written in Java. There's an API library called navappclient.jar which I included in the #additionaljar. i cannot figure out how to access the methods in this library. I know there is a tutorial "accessing third party Java" with an example, but frankly i am too dumb to translate this to the given API.
The API says the following:
/quote
To use the NavApp SDK you need to create an instance of the NavAppClient, using the NavAppClient.Factory.make(Context, ErrorCallback) method. The returned instance is used to access the individual APIs.

The package is called com.tomtom.navapp.

The interface (i shoul use) is called Routable
public interface Routeable
extends com.tomtom.navapp.internals.Data
This interface is holding the data relating to any geographic location on the map. A Routeable can contain a latitude/longitude, and an address associated with this point.
end quote/

And to begin with, i would like to call the methods double getLatitude() and double getLongitude()

Any help how to access these methods in b4a would be great, thank you.
Georg

P.S. I have b4a 3.82
 

gezueb

Active Member
Licensed User
Longtime User
Indeed, Erel, the error is gone! There's another one now, "unfortunatly, Navigations has stopped", but this is not anymore about the b4a/Java interface. I will now be a pain in the ass to the library builders.
Thank you so much. For other readers that might be interested I will post here the full code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: NavAppTest
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region
#AdditionalJar: navappclient

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub
Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
ToastMessageShow (GetContext,True)
Dim Factory As JavaObject
Factory.InitializeStatic("com.tomtom.navapp.NavAppClient.Factory")
Dim callback As Object = Factory.CreateEvent("com.tomtom.navapp.ErrorCallback", "NavError", Null)
Dim client As JavaObject = Factory.RunMethod("make", Array(GetContext, callback))
Dim lat = 33, lon = 33 As Double
Dim r1 As JavaObject = client.RunMethod("makeRouteable", Array(lat, lon))
ToastMessageShow (r1,True)
Dim locationManager As JavaObject = client.RunMethod("getLocationManager", Null)
Dim RouteableListener As JavaObject = locationManager.CreateEvent("com.tomtom.navapp.Routeable.Listener", "Listener", Null)
locationManager.RunMethod("getCurrentLocation", Array(RouteableListener))
End Sub   
Sub Listener_Event (MethodName As String, Args() As Object) As Object

Dim routable As JavaObject = Args(0)
Log(routable.RunMethod("getLatitude", Null))

End Sub


Sub NavError_Event (MethodName As String, Args() As Object) As Object
  Log(MethodName)
End Sub
Sub GetBA As JavaObject
  Dim jo As JavaObject
  Dim cls As String = Me
  cls = cls.SubString("class ".Length)
  jo.InitializeStatic(cls)
  Return jo.GetFieldJO("processBA")
End Sub

Sub GetContext As JavaObject
  Return GetBA.GetField("context")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
 
End Sub
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
Erel, I need some more of your help on this topic. The following code should activate a trip in the nav system
The last statement of the ..._click Sub throws exceptions which vary with the call. I have include all my calls
and there exceptions. The native Java code is also included (commented) in the code.
B4X:
Sub btnTrip_Click   
' planTrip, this is the java code from the test programm
'
'void planTrip(Routeable destination,
'              Trip.PlanListener PlanListener)
'
'    Plan a trip from the current location To a destination. This call will switch the focus To the NavApp when the trip-planning Is started. The passed In listener Is only active Until the result of the call has been returned. Once a result has been returned the listener won't receive any more callbacks (but can be re-used).
'
'    Parameters:
'        destination - The Routeable destination To plan To.
'        PlanListener - Trip.PlanListener To receive information about the result of the trip planning.
'    Throws:
'        java.lang.IllegalArgumentException - If any argument Is Null.

    'here my code begins
    'lat and lon are hard coded for tests (EPS)
    Dim lat As Double = 47.4642
    Dim lon As Double = 9.03826
    'init API and error callback
    Dim Factory As JavaObject
    Factory.InitializeStatic("com.tomtom.navapp.NavAppClient.Factory")
    Dim callback As Object = Factory.CreateEvent("com.tomtom.navapp.ErrorCallback", "NavError", Null)
    Dim client As JavaObject = Factory.RunMethod("make", Array(GetContext, callback))
    Dim RouteableDestination As JavaObject = client.RunMethod("makeRouteable", Array(lat, lon))
    Dim TripManager As JavaObject = client.RunMethod("getTripManager",Null)
    Dim PlanListener As JavaObject = TripManager.CreateEvent("com.tomtom.navapp.Trip.PlanListener", "ListenerTrip", Null)
'    These calls all throw an exception as noted belowthe call
'    TripManager.RunMethod("planTrip",Array(PlanListener))
'    java.lang.RuntimeException: Method: planTrip not matched.
'    TripManager.RunMethod("planTrip",Array(lat,lon))
'    java.lang.RuntimeException: Method: planTrip not matched.
'    TripManager.RunMethod("planTrip",Null)
'    TripManager.RunMethod("planTrip",PlanListener)
'  java.lang.IllegalArgumentException: argument 2 should have type java.lang.Object[], got $Proxy8
'    TripManager.RunMethod("planTrip",Array(RouteableDestination))
'  java.lang.RuntimeException: Method: planTrip not matched.
    TripManager.RunMethod("planTrip",RouteableDestination)
    'java.lang.ClassCastException: $Proxy7 cannot be cast to java.lang.Object[]

End Sub
Sub ListenerTrip_Event (MethodName As String, Args() As Object) As Object
    Msgbox("TripListener"&MethodName,"")
End Sub
I am grateful as ever for your help
Georg
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
Indeed, Erel, this works! Sounds a bit weird that one could squeeze two different items into a single array, but then they are Java objects! Might need your help with the geocoder too, but I try to figure out that first.
Thank you for your early morning help!
Georg
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
calling the Geocoder with an adress string should return a list of possible adresses which can be limited.
I am able to call the Geocoder, but on the ListListener at the statement
routeable.RunMethod I get an error:

java.lang.RuntimeException: Method: onRoutable not found in: java.util.ArrayList
This is the sub:
B4X:
Sub ListenerGeo_Event (MethodName As String, Args() As Object) As Object
'passes back a list of coordinates of an adress string
'this is the java code:
'    Private routeable.ListListener mGeoCodeRouteableListener = new routeable.ListListener() {
'        @Override
'        Public void onRoutable(List<routeable> routeableList) {
'            Log.d(TAG, "> onRoutable received a List with size["+routeableList.size()+"]");
'            mButtonGeoCode.setEnabled(True);
'            CharSequence previousText = mTextViewGeoCodeResult.getText();
'            mTextViewGeoCodeResult.setText(createAddressString(previousText, routeableList));
'        }
'    };
    Dim routeable As JavaObject = Args(0)
    routeable.RunMethod("onRoutable",Null) 'this triggers the exception
    lat = routeable.RunMethod("getLatitude", Null) 'not sure if this would work on a list
    lon = routeable.RunMethod("getLongitude",Null)
    EditLat.Text = lat
    EditLon.Text = lon
End Sub

here is the interface description:
Interface Routeable.ListListener
All Superinterfaces:
com.tomtom.navapp.internals.Callback
Enclosing interface:
Routeable
public static interface Routeable.ListListener
extends com.tomtom.navapp.internals.Callback
Listener to get an update of a List of Routeable objects. All callbacks are performed on the UI Thread.






Method Summary
void onRoutable(java.util.List<Routeable> routeable)
Callback for any requests related to the Routeable object that need to return more than one Routeable object.


Method Detail
onRoutable
void onRoutable(java.util.List<Routeable> routeable)
Callback for any requests related to the Routeable object that need to return more than one Routeable object.


Parameters:
routeable - The requested List of Routeable, the List can be empty but not null, see the relevant API for the reason.


best regards,
Georg
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
Erel. I have found a workabout. The routable passed back is nothing else than a rather complex string. I parsed it with my own code and it works:
B4X:
Sub ListenerGeo_Event (MethodName As String, Args() As Object) As Object

    Dim routeable As JavaObject = Args(0)
    Dim Adressstring As String
    Dim Searchfor As String = "getLongitude"
    Dim Startindex As Int
    Dim EndIndex As Int
'    let's parse the f..cking latitude and longitude as the proposed method is a bit temperamentful
'    luckily Basic 4 Apps has wonderfull string functions
    Adressstring = routeable 'the routable is nothing else than a complex string
    If Adressstring.Length > 14 Then    'if it is shorter then there are no coordinates
        Startindex = Adressstring.IndexOf(Searchfor)
        Startindex = Startindex + 14
        EndIndex = Startindex+ 7
        lon = Adressstring.SubString2(Startindex,EndIndex)

        Searchfor = "getLatitude"
        Startindex = Adressstring.IndexOf(Searchfor)
        Startindex = Startindex +13
        EndIndex = Startindex + 7
        lat = Adressstring.SubString2(Startindex,EndIndex)
       
        EditLat.Text = lat
        EditLon.Text = lon
    Else
        Msgbox ("Adress invalid","")
    End    If   
End Sub

So all my problems for the time being are solved with your valuable help. Thank you very much indeed,
Georg
 
Upvote 0
Top