Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - simple user input form web-view to back-end java with jQuery Mobile

I am currently designing a native android application. I am planning to use jQuery Mobile web view as my interface and do all the calculations with java back-end. (still deciding using phonegap or not)

I have some difficulties implementing a page that allows a user to fill in a form and pass the variable to the android java part.

Researching all morning, I have learned how to interact between javascript/html and java with addJavascriptInterface(). But the only thing I can find that answer my question is with JSON. That seems a little complicated. Is there a way I can pass the variable as a parameter of a java function?

(I learned that if I do not use a web view, I can simply use getText() or getSelectedItem() with default UI to do what I want to)

I apologize there is no code avaliable, since this is still in designing stage, and I am a little new to android sdk.

Thanks

like image 996
AbSoLution8 Avatar asked Jan 22 '12 13:01

AbSoLution8


1 Answers

OK, here's an example of interacting with the javascript interface...

Setting the javascript interface in your Activity...

JavascriptInterface javasriptInterface = new JavascriptInterface(this);
webview.addJavascriptInterface(javasriptInterface, "Android");

The inner JavascriptInterface class in your Android Activity...

public class JavascriptInterface {
    Context mContext;

    JavascriptInterface(Context c) {
        mContext = c;
    }

    public boolean doSomething(String name, String address) {
        ...
        return true;
    }
}

EDIT: Your form will have various input fields. Example...

<form name="myForm" ...>
    <input type=text name=personName>
    <input type=text name=personAddress>
    <input type="button" value="Do it" onClick="callDoSomething()" />
</form>

<script type="text/javascript">
    function callDoSomething() {
        var theName = document.myForm.personName.value;
        var theAddress = document.myForm.personAddress.value;
        var result = Android.doSomething(theName, theAddress);
    }
</script>
like image 62
Squonk Avatar answered Sep 27 '22 19:09

Squonk