evaluatejavascript不起作用_如何evaluateJavascript工作?

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() {

@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?

Update: Little bit more info:

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

Devices: Both Nexus 7 and Nexus 5 (Stock)

解决方案

There is an example of the evaluateJavascript method being used in this sample:

Essentially if the javascript you execute in the WebView returns a value it'll be passed in the callback.

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.

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() {

@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

}

}

}

});

}

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.

For example if you went:

mWebView.evaluateJavascript("(function() { return 'this'; })();", new ValueCallback() {

@Override

public void onReceiveValue(String s) {

Log.d("LogName", s); // Prints: "this"

}

});

It would print the string this, wrapped in double quotes: "this".

Other examples worth noting:

mWebView.evaluateJavascript("(function() { return null; })();", new ValueCallback() {

@Override

public void onReceiveValue(String s) {

Log.d("LogName", s); // Prints the string 'null' NOT Java null

}

});

mWebView.evaluateJavascript("(function() { })();", new ValueCallback() {

@Override

public void onReceiveValue(String s) {

Log.d("LogName", s); //s is Java null

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值