1

I am calling JavaScript via WebView. This is my code:

public class MyWebViewClient extends WebViewClient {
.....

   @Override
   public void onPageFinished(final WebView view, final String url) {
       super.onPageFinished(view, url);
       view.loadUrl("javascript:Android.showAdBanner(showSdkAd())");
       view.loadUrl("javascript:Android.updateUrl(appUrl())");
   }
}


public class WebViewJavaScriptInterface {
   ....

    @JavascriptInterface
    public void showAdBanner(boolean appUrl) {
       if (appUrl) {
            ...
       } else {.....}
    }

    @JavascriptInterface
    public void updateUrl(String appUrl) {

       if (appUrl.isEmpty()) {
         ....
       }

       if (appUrl.equals("m.url.de")) {
         ....
       }
    }
}

showSdkAd() returns a boolean value and appUrl() returns a String.

The problems is that I get the wrong values, always false when the showSdkAd() function is called and an empty String from appUrl().

I've already checked it and it works on the server side, so the problem should be on my side.

Does anybody have an idea?

IrApp
  • 1,823
  • 5
  • 24
  • 42
  • 1
    Try to replace with this view.loadUrl("javascript:Android.showAdBanner(showSdkAd())"); to view.loadUrl(your_url); – Amsheer May 05 '15 at 13:35
  • Actually I don´t know the Url, it is given by the appUrl() JS function. – IrApp May 05 '15 at 13:42
  • Try temporarily substituting the calls to `showSdkAd()` and `appUrl()` with some constants, e.g. `true` and `http://example.com` respectively in your `onPageFinished` code, and see whether you are getting those values in your injected object correctly. It is possible that you are calling `showSdkAd()` and `appUrl()` too early. Also note that in Java you need to compare Strings using `equals` function, e.g. `"m.url.de".equals(appUrl)`, see http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Mikhail Naganov May 05 '15 at 17:20
  • When I substitute the calls with some constants it works :( . Thanks for the type about compare strings! – IrApp May 06 '15 at 07:39

0 Answers0