1

I have two class and I want to get 'JSI.var' in MainActivity. Firstly i getting "" empty value when i button clicked. So, no log. Secondly i getting document.getElementsByName('name')[4].src) value. So, there are document.getElementsByName('name')[4].src) in next logs. How i get another class in @JavascriptInterface method.

in onCreate() method in MainActivity

  webview.getSettings().setJavaScriptEnabled(true);
  webview.addJavascriptInterface(new JSI(), "HTMLOUT");
  bt1.setOnClickListener(new OnClickListener(){
        public void onClick(View v){        
            for(int i=0;i<5;i++){
             webview.loadUrl("javascript:window.HTMLOUT.processHTML(document.getElementsByName('name')["+i+"].src);");
             Log.i("CODE : ",JSI.var);
             //or
             String str = JSI.var;}
        }});

JSI class

import android.webkit.JavascriptInterface;

public class JSI
{
    static String var="";

 @JavascriptInterface
 public static void processHTML(String html)
{    
    var=html;
 }
 }
2
  • May be the problem is that the Log.i is executed before the javascript, try to put a delay before the Log, or Log in the processHTML method Commented Jan 22, 2015 at 10:08
  • if i put Log.i--->in processHTML(String html) method. Log works correctly. but i want to get 'var' and apply Log.i(var); in MainActivity Commented Jan 22, 2015 at 10:09

1 Answer 1

1

Try this

static String var="";

public void onCreate() {
webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(this, "HTMLOUT");
bt1.setOnClickListener(new OnClickListener(){
    public void onClick(View v){        
        for(int i=0;i<5;i++){
    webview.loadUrl("javascript:window.HTMLOUT.processHTML(document.getElementsByName('name')["+i+"].src);");
        }
    }
);
}

@JavascriptInterface
 public static void processHTML(String html){    
    var=html;
    Log.i("CODE : ",var);
 }     

This way you have the value in your activity when the javascript is executed.

Sign up to request clarification or add additional context in comments.

2 Comments

its worked but i want to get 'JSI.var' in MainActivity. Because i equal 'JSI.var' to String variable in MainActivity.
But if you put all in the MainActivity, even the processHTML method, you get what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.