Pass an array from android to JavaScript - Stack Overflow

I went through all the posts shared in here, I still couldn't find my proper answer to run my code

I went through all the posts shared in here, I still couldn't find my proper answer to run my code and make my app functional. I'll appreciate it if any of you who could help me on this issue.

I am calling bounded function getLocation() in tages as mentioned: . The getLocation function is bounded to MyHandler in Android as mentioned in below:

public class MainActivity extends Activity {

    WebView webView;
    private Handler mHandler = new Handler ();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.addJavascriptInterface (new Object(){
                       public void getLocation(){
                          mHandler.post (new Runnable(){
                            public void run(){
                               String[] values = {"3.16802","101.71309"};
                               webView.loadUrl("javascript:setLocation("+values+")");
                            }
                          });
                       }
                }, "MyHandler");
    webView.loadUrl("file:///android_asset/example/quick-start-example.html");

    }
}

The function setLocation in Android calls the method in JavaScrip as below, here we are passing the array into JS function:

function setLocation(val){
    var lat = val[0];
    var lng = val[1];
}

Please advice on this. Thank you.

I went through all the posts shared in here, I still couldn't find my proper answer to run my code and make my app functional. I'll appreciate it if any of you who could help me on this issue.

I am calling bounded function getLocation() in tages as mentioned: . The getLocation function is bounded to MyHandler in Android as mentioned in below:

public class MainActivity extends Activity {

    WebView webView;
    private Handler mHandler = new Handler ();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.addJavascriptInterface (new Object(){
                       public void getLocation(){
                          mHandler.post (new Runnable(){
                            public void run(){
                               String[] values = {"3.16802","101.71309"};
                               webView.loadUrl("javascript:setLocation("+values+")");
                            }
                          });
                       }
                }, "MyHandler");
    webView.loadUrl("file:///android_asset/example/quick-start-example.html");

    }
}

The function setLocation in Android calls the method in JavaScrip as below, here we are passing the array into JS function:

function setLocation(val){
    var lat = val[0];
    var lng = val[1];
}

Please advice on this. Thank you.

Share Improve this question asked Jul 29, 2013 at 17:58 Amin AminianAmin Aminian 411 silver badge4 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 1

I don't think you can easily do a plex object like an Array. You are starting to get into JSON territory.

In fact, I found some sample code that uses JSON to acplish this.

Problem: You want to push array data from Java to JavaScript and/or from JavaScript to Java http://androidcookbook./Recipe.seam?recipeId=4426&title=Exchanging%20Array%20Data%20between%20Java%20and%20JavaScript

Whenever you are passing object like Array or List in Javascript function then first, you need to convert that object into JSON format like below:

 String[] values = {"3.16802","101.71309"};
 webView.loadUrl("javascript:setLocation('"+new JSONArray(values)+"')");

And in Java script function again you have to parse that JSON format input like below:

function setLocation(input){
var value=JSON.parse(input);
var lat = value[0];
var lng = value[1];
}

^above ment, you can use onPageFinished to execute the loadurl part once your page is done loading like this:

myWebView.setWebViewClient(new WebViewClient(){
    public void onPageFinished(WebView view, String url){
        myWebView.loadUrl("javascript:setLocation("+values+")");
    }
});
 @JavascriptInterface
    public String[] displayName() {
        String ar[] = new String[2];
        ar[0]= "name1";
        ar[1] =  "name2";
        return ar;
    }

or to get Data in js function

   var t1 = Android.displayName();

You should probably try this:

Gson gson = new Gson();
String data = gson.toJson(someArray);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745456812a4628524.html

相关推荐

  • Pass an array from android to JavaScript - Stack Overflow

    I went through all the posts shared in here, I still couldn't find my proper answer to run my code

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信