如何evaluateJavascript工作? [英] How does evaluateJavascript work?
问题描述
我尝试使用新的<一个href="http://developer.android.com/reference/android/webkit/WebView.html#evaluateJavascript%28java.lang.String,%20android.webkit.ValueCallback%3Cjava.lang.String%3E%29">evaluateJavascript法Android 4.4系统,但所有我曾经得到的回复是一个空的结果:
I'm trying to use the new evaluateJavascript method in Android 4.4, but all I ever get back is a null result:
webView1.evaluateJavascript("return \"test\";", new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
Log.d("LogName", s); // Log is written, but s is always null
}
});
我如何将结果返回给该方法?
How do I return a result to this method?
更新:多一点信息:
- 我在互联网权限集
- 我
setJavascriptEnabled(真);
- 在尝试撇字符串:
返回测试;
, - 在试过JS对象:
返回{测试:这个'}
-
的console.log('测试');
正在执行细 - 设置
targetSdkVersion
19按: 如果您的应用程序使用的WebView 的
- I have the INTERNET permission set
- I have
setJavascriptEnabled(true);
- Tried apostrophe string:
return 'test';
, - Tried JS object:
return { test: 'this' }
console.log('test');
is being executed fine.- Set
targetSdkVersion
to 19 as per: If your app uses WebView
设备:两者的Nexus 7和Nexus 5(股票)
Devices: Both Nexus 7 and Nexus 5 (Stock)
推荐答案
目前正在此示例中使用的evaluateJavascript方法的一个例子:
There is an example of the evaluateJavascript method being used in this sample:
<一个href="https://github.com/GoogleChrome/chromium-webview-samples/tree/master/jsinterface-example">https://github.com/GoogleChrome/chromium-webview-samples/tree/master/jsinterface-example
从本质上讲,如果你在web视图执行JavaScript返回它会在回调传递一个值。
Essentially if the javascript you execute in the WebView returns a value it'll be passed in the callback.
主要的是要注意的是,在OnReceiveValue返回的字符串或者是一个JSON值,JSON对象,或JSON数组取决于你回什么。
The main thing to note is that the String returned in OnReceiveValue is either a JSON Value, JSON Object or JSON Array depending on what you return.
这里需要注意,这是如果返回一个值,你需要使用setLenient(真)在一个JSON读者为它工作。
Things to note about this is if you return a single value, you need to use setLenient(true) on a JSON reader for it to work.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// In KitKat+ you should use the evaluateJavascript method
mWebView.evaluateJavascript(javascript, new ValueCallback<String>() {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onReceiveValue(String s) {
JsonReader reader = new JsonReader(new StringReader(s));
// Must set lenient to parse single values
reader.setLenient(true);
try {
if(reader.peek() != JsonToken.NULL) {
if(reader.peek() == JsonToken.STRING) {
String msg = reader.nextString();
if(msg != null) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
}
}
} catch (IOException e) {
Log.e("TAG", "MainActivity: IOException", e);
} finally {
try {
reader.close();
} catch (IOException e) {
// NOOP
}
}
}
});
}
您可能仍想使用一个解析器的字符串响应的原因是它被转换为一个JSON值,这意味着它将被包裹在引号。
The reason you may still want to use a parser for a string response is it is converted to a JSON value which means it will be wrapped in quotes.
例如,如果你去:
mWebView.evaluateJavascript("(function() { return 'this'; })();", new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
Log.d("LogName", s); // Prints: "this"
}
});
这将打印字符串这一点,双引号括起来:这个
It would print the string this, wrapped in double quotes: "this".
其他值得注意的例子:
mWebView.evaluateJavascript("(function() { return null; })();", new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
Log.d("LogName", s); // Prints the string 'null' NOT Java null
}
});
mWebView.evaluateJavascript("(function() { })();", new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
Log.d("LogName", s); //s is Java null
}
});
这篇关于如何evaluateJavascript工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!