Calls from javascript to java must be asynchronous. The way to do this is
to:
// You want javascript to be able to call into java AND receive a result.
// From the Java side, you would add a callback
bc.addJSCallback("window.myfunc=function(arg){callback.onSuccess(arg)}",
res->{
String[] parts = Util.split(res.toString(), " ");
String arg = parts[0];
String callbackFunc = parts[1];
String result = doSomethingWithArg(arg);
bc.execute(callbackFunc+"("+result+")");
});
// On the javascript side you could then do something like this
window.myCallback = function(arg) {
// Do something with the argument
}
myfunc("myarg window.myCallback");
This is a simplistic example that passes a a message from javascript to
java by placing the "contents" of the message at the beginning, and then a
callback to call in javascript after the space. If you need to send
arbitrary strings, then this scheme will fail - but you could just as
easily send a JSON message from javascript that includes some content and
information about where to send it (the callback information), which would
be more resilient to argument formatting.
Steve
On Thu, Jan 4, 2018 at 11:24 AM, <[email protected]> wrote:
> Thanks Steve
>
> on the point 1 I need to call from Javascript to Java, and return from
> Java to Javascript a response.
>
> My doubt is how do I have to do it in Java and in Javascritp, I searched
> examples in code of GoolgeMaps Implementation but I didn't find any help.
>
> Best Regards Kandy
>
> On Thursday, January 4, 2018 at 3:53:19 PM UTC+1, Steve Hannah wrote:
>
>> The best examples right now are in the GoogleMaps cn1lib
>> https://github.com/codenameone/codenameone-google-maps/blob/
>> master/GoogleMaps/src/com/codename1/googlemaps/MapContainer.java
>>
>> There are also samples in the unit tests:
>> https://github.com/codenameone/CodenameOne/blob/master/
>> tests/core/test/com/codename1/ui/JavascriptTests.java
>>
>> The unit tests all use the executeAndWait() wrappers for convenience, but
>> these carry the baggage of invokeAndBlock - to make it synchronous. Best
>> for performance to try to use async versions whenever possible.
>>
>> 1) call from Javascritp to java and receive a response from java
>>
>>
>> First add your callback:
>> bc.addJSCallback("window.app.mycb=function(arg){callback.onSuccess(arg)}",
>> arg->{
>> // Log.p("The result was "+arg.getInt());
>> });
>>
>> Then on the javascript side, you can call "window.app.mycb(1+2)", and
>> your java callback will be called.
>>
>> 2) call from java to javascript and receive a response from javascript
>>
>>
>> Async style:
>>
>> bc.execute("callback.onSuccess(1+2)", res->{
>> Log.p("The result was "+res.getInt());
>> });
>>
>> Sync Style:
>>
>> int result = bc.executeAndWait("callback.onSuccess(1+2)").getInt();
>>
>>
>> If you need to pass parameters to your javascript code, there are
>> variants that do that and handle escaping strings properly.
>>
>> e.g.
>>
>> bc.execute("callback.onSuccess(${0}+${1})", new Object[]{1, 2}, res->{
>> Log.p("The result was "+res.getInt()); // Should be 3
>> });
>>
>> If you need to call methods on a Javascript object, you can also create a
>> java "Proxy" object to make this a bit easier. E.g.
>>
>> JSProxy window = bc.createJSProxy("window");
>> int height = window.getAndWait("innerHeight").getInt();
>>
>>
>> Steve
>>
>> On Thu, Jan 4, 2018 at 6:12 AM, <[email protected]> wrote:
>>
>>> Hi Steve
>>>
>>> where can I found documentation or examples to addJSCallback?
>>>
>>> I can be able to:
>>>
>>> call from javascritp to java
>>>
>>> browserComponent.addJSCallback(
>>> "window.app = {isActive: function() {callback.onSuccess()}}",
>>> value -> {
>>> call from java to javacript and send parameters
>>>
>>> browserComponent.execute("controlPopUp ('"+ relojReservaValueString
>>> +"', "+ reservationState +","+ vehicleLockState +")");
>>>
>>>
>>> Now I need,
>>>
>>> 1) call from Javascritp to java and receive a response from java
>>>
>>> 2) call from java to javascript and receive a response from javascritp
>>>
>>>
>>> Can you help me ?
>>>
>>> Best Regards Kandy
>>>
>>> On Wednesday, January 3, 2018 at 8:05:56 PM UTC+1, [email protected]
>>> wrote:
>>>>
>>>> Hi Steve:
>>>>
>>>> Indeed, upon further inspection you're so right! We've just detected
>>>> a problem with our SVN workflow in which we keep updating the "libVersion"
>>>> property on codenameone.properties between dev machines WITHOUT updating
>>>> the libs at the same time, so we were keeping old libs and preventing the
>>>> proper update procedure.
>>>>
>>>> We now see the deprecated warnings and the new code in
>>>> BrowserComponent. Will work from here.
>>>>
>>>> Kind regards,
>>>> Miguelanxo.
>>>>
>>>> On Wednesday, January 3, 2018 at 7:09:50 PM UTC+1, Steve Hannah wrote:
>>>>>
>>>>> I just confirmed with a fresh project. The changes are there. I'm
>>>>> not sure why you're not picking them up. Is your project using the libs
>>>>> at
>>>>> the default location (lib/CodenameOne.jar and JavaSE.jar) or have you
>>>>> adjusted the classpath for the project.
>>>>>
>>>>> Steve
>>>>>
>>>>> On Wed, Jan 3, 2018 at 10:02 AM, <[email protected]> wrote:
>>>>>
>>>>>> Hi Steve
>>>>>>
>>>>>> I already did the project libs update, but didn't help. My library
>>>>>> date is 30/12/2017, when I do the update it says "Your proyect Libs are
>>>>>> up
>>>>>> to date", we are blocked on the development of our App to IOS.
>>>>>>
>>>>>> Please help us.
>>>>>>
>>>>>> Best Regards
>>>>>> Kandy
>>>>>>
>>>>>> On Wednesday, January 3, 2018 at 5:44:23 PM UTC+1, Steve Hannah wrote:
>>>>>>
>>>>>>>
>>>>>>> On Wed, Jan 3, 2018 at 8:35 AM, <[email protected]> wrote:
>>>>>>>
>>>>>>>> Hello:
>>>>>>>>
>>>>>>>> How can we use that new interface? There's no documentation and
>>>>>>>> our current lib doesn't expose said addJSCallback() yet (and updating
>>>>>>>> c1
>>>>>>>> libs didn't help).
>>>>>>>>
>>>>>>> You need to update your project libs. It should be there (just
>>>>>>> tested on a project here). (I.e. Project Properties > Update Project
>>>>>>> Libs
>>>>>>> .. not "update cn1libs")
>>>>>>>
>>>>>>>
>>>>>>>> Also, is the fix already available in the build servers?
>>>>>>>>
>>>>>>> No. It will be up in the next build server update on Friday.
>>>>>>>
>>>>>>> Steve
>>>>>>>
>>>>>>>
>>>>>>>>
>>>>>>>> Kind regards,
>>>>>>>> Kandy.
>>>>>>>>
>>>>>>>>
>>>>>>>> On Wednesday, January 3, 2018 at 2:47:04 PM UTC+1, Steve Hannah
>>>>>>>> wrote:
>>>>>>>>>
>>>>>>>>> This looks like a race condition that has been there for a while.
>>>>>>>>> I have just committed a fix for it here
>>>>>>>>> https://github.com/codenameone/CodenameOne/commit/783ac28284
>>>>>>>>> 09aa747103a35001b4886e24944484
>>>>>>>>>
>>>>>>>>> With the last update, however, the com.codename1.javascript
>>>>>>>>> package is now deprecated in favour of a new async API that is
>>>>>>>>> incorporated
>>>>>>>>> directly into the BrowserComponent class. I recommend migrating over
>>>>>>>>> to
>>>>>>>>> this new API for new code, as it will produce much better performance
>>>>>>>>> (no
>>>>>>>>> hidden invokeAndBlock under the hood - except in xxxAndWait()
>>>>>>>>> methods).
>>>>>>>>>
>>>>>>>>> Using the new API, your code would become:
>>>>>>>>>
>>>>>>>>> bc.addJSCallback("window.checkfoxClickedCallback ={setChecked:
>>>>>>>>> function(){ callback.onSuccess(null)}}", r->{
>>>>>>>>> Log.p("->checkboxClickedCallback.setChecked()");
>>>>>>>>> contexto.mostrarMensaje("checkboxClickedCallback.setChecked(
>>>>>>>>> )");
>>>>>>>>> });
>>>>>>>>> bc.addJSCallback("window.okButtonClickedCallback = {showMap:
>>>>>>>>> function() { callback.onSuccess(null)}}", r->{
>>>>>>>>> Log.p("->okButtonClickedCallback.showMap()");
>>>>>>>>> contexto.mostrarMensaje("okButtonClickedCallback.showMap()");
>>>>>>>>> });
>>>>>>>>>
>>>>>>>>> Notice, with this new syntax, you reference the java callback
>>>>>>>>> explicitly in the javascript code via the "callback" variable, which,
>>>>>>>>> in
>>>>>>>>> Javascript, contains 2 methods: onSuccess(arg), and onError(message,
>>>>>>>>> code).
>>>>>>>>>
>>>>>>>>> Steve
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> On Wed, Jan 3, 2018 at 5:09 AM, <[email protected]> wrote:
>>>>>>>>>
>>>>>>>>>> Hello:
>>>>>>>>>>
>>>>>>>>>> Since the last codename1 libary update we've been having
>>>>>>>>>> problems with the BrowserComponent on iOS. We've narrowed down the
>>>>>>>>>> problem
>>>>>>>>>> to the callback registration process (which gets executed on the
>>>>>>>>>> BrowserComponent onLoad callback). We register two functions, but
>>>>>>>>>> only the
>>>>>>>>>> second one can be invoked from javascript. The first one gets
>>>>>>>>>> registered ok
>>>>>>>>>> (we can see its code from the javascript side) but fails to execute
>>>>>>>>>> on iOS
>>>>>>>>>> (in Android and the simulator works fine). We tried a lot of
>>>>>>>>>> permutations,
>>>>>>>>>> and different argument lengths for both functions, but the only
>>>>>>>>>> difference
>>>>>>>>>> we see is the registration order. If we switch the registration
>>>>>>>>>> order, we
>>>>>>>>>> can only invoke the other one (whichever gets registered last).
>>>>>>>>>>
>>>>>>>>>> /*
>>>>>>>>>> * Registramos el callback JS de valor cambiado del checkbox "No
>>>>>>>>>> volver a mostrar este asistente".
>>>>>>>>>> */
>>>>>>>>>> JSObject checkboxClickedCallback = (JSObject)
>>>>>>>>>> javascriptContext.get("{}");
>>>>>>>>>> checkboxClickedCallback.set("setChecked", new JSFunction() {
>>>>>>>>>> public void apply(JSObject self, Object[] args) {
>>>>>>>>>> Log.p("->checkboxClickedCallback.setChecked()");
>>>>>>>>>> contexto.mostrarMensaje("checkboxClickedCallback.setChecked()");
>>>>>>>>>>
>>>>>>>>>> }
>>>>>>>>>> });
>>>>>>>>>> javascriptContext.set("window.checkboxClickedCallback",
>>>>>>>>>> checkboxClickedCallback);
>>>>>>>>>>
>>>>>>>>>> /*
>>>>>>>>>> * Registramos el callback JS de click en el botón "OK".
>>>>>>>>>> */
>>>>>>>>>> JSObject okButtonClickedCallback = (JSObject)
>>>>>>>>>> javascriptContext.get("{}");
>>>>>>>>>> okButtonClickedCallback.set("showMap", new JSFunction() {
>>>>>>>>>> public void apply(JSObject self, Object[] args) {
>>>>>>>>>> Log.p("->okButtonClickedCallback.showMap()");
>>>>>>>>>> contexto.mostrarMensaje("okButtonClickedCallback.showMap()");
>>>>>>>>>>
>>>>>>>>>> }
>>>>>>>>>> });
>>>>>>>>>> javascriptContext.set("window.okButtonClickedCallback",
>>>>>>>>>> okButtonClickedCallback);
>>>>>>>>>>
>>>>>>>>>> Please help, as it's impacting our current production code.
>>>>>>>>>>
>>>>>>>>>> Kind regards,
>>>>>>>>>> Kandy
>>>>>>>>>>
>>>>>>>>>> --
>>>>>>>>>> You received this message because you are subscribed to the
>>>>>>>>>> Google Groups "CodenameOne Discussions" group.
>>>>>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>>>>>> send an email to codenameone-discussions+unsubs
>>>>>>>>>> [email protected].
>>>>>>>>>> Visit this group at https://groups.google.com/grou
>>>>>>>>>> p/codenameone-discussions.
>>>>>>>>>> To view this discussion on the web visit
>>>>>>>>>> https://groups.google.com/d/msgid/codenameone-discussions/49
>>>>>>>>>> 3a9217-0a4a-44b5-aede-974096f652d4%40googlegroups.com
>>>>>>>>>> <https://groups.google.com/d/msgid/codenameone-discussions/493a9217-0a4a-44b5-aede-974096f652d4%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>>>>>>> .
>>>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> --
>>>>>>>>> Steve Hannah
>>>>>>>>> Software Developer
>>>>>>>>> Codename One
>>>>>>>>> http://www.codenameone.com
>>>>>>>>>
>>>>>>>> --
>>>>>>>> You received this message because you are subscribed to the Google
>>>>>>>> Groups "CodenameOne Discussions" group.
>>>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>>>> send an email to codenameone-discussions+unsubs
>>>>>>>> [email protected].
>>>>>>>> Visit this group at https://groups.google.com/grou
>>>>>>>> p/codenameone-discussions.
>>>>>>>> To view this discussion on the web visit
>>>>>>>> https://groups.google.com/d/msgid/codenameone-discussions/0a
>>>>>>>> a6820b-d038-4a3f-82ad-c45fb4c6d5e6%40googlegroups.com
>>>>>>>> <https://groups.google.com/d/msgid/codenameone-discussions/0aa6820b-d038-4a3f-82ad-c45fb4c6d5e6%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>>>>> .
>>>>>>>>
>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> Steve Hannah
>>>>>>> Software Developer
>>>>>>> Codename One
>>>>>>> http://www.codenameone.com
>>>>>>>
>>>>>> --
>>>>>> You received this message because you are subscribed to the Google
>>>>>> Groups "CodenameOne Discussions" group.
>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>> send an email to [email protected]
>>>>>> .
>>>>>> Visit this group at https://groups.google.com/grou
>>>>>> p/codenameone-discussions.
>>>>>> To view this discussion on the web visit
>>>>>> https://groups.google.com/d/msgid/codenameone-discussions/f7
>>>>>> 466bae-c35d-4baf-a776-ecc9e13f65ea%40googlegroups.com
>>>>>> <https://groups.google.com/d/msgid/codenameone-discussions/f7466bae-c35d-4baf-a776-ecc9e13f65ea%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>>> .
>>>>>>
>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Steve Hannah
>>>>> Software Developer
>>>>> Codename One
>>>>> http://www.codenameone.com
>>>>>
>>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "CodenameOne Discussions" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> Visit this group at https://groups.google.com/grou
>>> p/codenameone-discussions.
>>> To view this discussion on the web visit https://groups.google.com/d/ms
>>> gid/codenameone-discussions/014fa1a2-ef58-49ba-bf93-546a9589
>>> 6146%40googlegroups.com
>>> <https://groups.google.com/d/msgid/codenameone-discussions/014fa1a2-ef58-49ba-bf93-546a95896146%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
>>
>> --
>> Steve Hannah
>> Software Developer
>> Codename One
>> http://www.codenameone.com
>>
> --
> You received this message because you are subscribed to the Google Groups
> "CodenameOne Discussions" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> Visit this group at https://groups.google.com/
> group/codenameone-discussions.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/codenameone-discussions/04c278eb-ece6-40bc-8135-
> 8dbaefd4d7c7%40googlegroups.com
> <https://groups.google.com/d/msgid/codenameone-discussions/04c278eb-ece6-40bc-8135-8dbaefd4d7c7%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>
--
Steve Hannah
Software Developer
Codename One
http://www.codenameone.com
--
You received this message because you are subscribed to the Google Groups
"CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit
https://groups.google.com/d/msgid/codenameone-discussions/CAGOYrKXuXyfX2v3mkkUw%3D62urGbpmXwAakV2ek8R1igtqXGm8A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.