Can't send a variable to javascript from java -
i'm fetching url , trying send it's text content javascript function inside webview.
i java code:
httpclient client = new defaulthttpclient(); httpget request = new httpget("http://volteck.net"); // response responsehandler<string> responsehandler = new basicresponsehandler(); string response_str = client.execute(request, responsehandler); response_str = response_str.replaceall("\n", "\\"); // because js don't support multi-line strings mywebview.loadurl("javascript:myfunction('" + response_str + "')"); system.out.println("javascript:myfunction('" + response_str + "')");
this code inside webview:
function myfunction(val){ document.body.innerhtml = val.length; }
so far i'm trying text length. when use url of previous code, nothing happens. , log:
08-19 23:55:03.141: i/dalvikvm-heap(1846): grow heap (frag case) 3.551mb 262160-byte allocation 08-19 23:55:03.351: d/dalvikvm(1846): gc_for_malloc freed 0 objects / 0 bytes in 209ms
but weird thing if attempt fetch volteck.net/ip, contains ip number, works fine!
thanks lot java newbie.
try this:
scriptenginemanager manager = new scriptenginemanager(); scriptengine engine = manager.getenginebyname("javascript"); string script = "function myfunction(val){ document.body.innerhtml = val.length; }"; engine.eval(script); invocable inv = (invocable) engine; inv.invokefunction("myfunction", response_str );
Comments
Post a Comment