-1

I am trying to run Java function which is a member of my Android app class from Javascript. My function that I am trying to run is:

public static String printName(){
        return("Hello World!");
    }

And my javascript code to call this method is:

<!DOCTYPE html>
<html>
<body>

<h2>Example Webpage</h2>

<p id="demo"></p>

<script>
    function foo(){
        var name = Packages.com.example.joey.myproject.MainActivity.printName();
        return name;
    }
document.getElementById("demo").innerHTML = foo();
</script>

</body>
</html>

When I run this code with a web server nothing happens. HTML part like header is working good but Javascript part isn't running. I read this (calling java method in javascript) post but I can't run JSP files for now. Please help me with the Javascript.

abdllhcay
  • 39
  • 6
  • 1
    How exactly are you running this code that you expect Android functionality? Are you hosting this on a web server somewhere and accessing it from a browser on your Android device? Are you hosting this on a web server *on your Android device* and accessing it from the browser on that device? Are you hosting this within the context of some Android application and accessing it from elsewhere within that application? Something else? Please clarify why you *think* this should work and what you think it should do. – David Aug 28 '18 at 14:02
  • 1
    “nothing happens”? Not even the error _“`ReferenceError`: `Packages` is not defined”_ is thrown and shown in the console? Use the [browser console (dev tools)](https://webmasters.stackexchange.com/q/8525) (hit `F12`) and read any errors. – Sebastian Simon Aug 28 '18 at 14:03
  • @David Thanks for your reply. I am running a web server on my Android phone and yes I am accessing from my phone's web browser – abdllhcay Aug 28 '18 at 14:08
  • @xufox I got this error: ReferenceError: Packages is not defined – abdllhcay Aug 28 '18 at 14:10
  • @eternal_peace: *"I am running a web server on my Android phone"* - Then what you have is *two* applications. One is the web server app, the other is the in-browser JavaScript. The server-side app (very likely using JSP, though I imagine there are other options) would contain this functionality and expose it as needed (likely through an AJAX operation). The in-browser app would invoke that AJAX operation. – David Aug 28 '18 at 14:33
  • Where does your Java code come into all of this? Did you build the server app yourself and you're trying to call a Java method inside it? Or do you just have some Java code somewhere that you are trying to use in your JavaScript code? – Leo Aso Aug 28 '18 at 16:03

1 Answers1

1

The answer to your question is given here

Since JavaScript is used at Client Side, to do this, you need to use a basic of AJAX Query Also you can use basic JSP to do so.

Anchit
  • 171
  • 1
  • 11
  • I read this post as I mentioned before. Is there any other ways except JSP and AJAX? – abdllhcay Aug 28 '18 at 14:21
  • I don't find any other way to do so. Visit [this](https://bytes.com/topic/javascript/answers/529823-calling-java-within-javascript) link for more help – Anchit Aug 28 '18 at 14:29