简体   繁体   English

执行此javascript函数并获取返回值

[英]Execute this javascript function and get a return value

I have this android code that get return value from a javascript function, I have this code: 我有这个从javascript函数获取返回值的android代码,我有以下代码:

file_list_detail_webview1.evaluateJavascript("javascript:getValue();", new ValueCallback<String>() {
                @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) {
                                    FileFunction fileFunction = new FileFunction();
                                    try {
                                        fileFunction.writeFile(filePath, msg);
                                    } catch (ArcAppException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    } catch (IOException e) {
                        Log.e("TAG", "MainActivity: IOException", e);
                    } finally {
                        try {
                            reader.close();
                        } catch (IOException e) {
                            // NOOP
                        }
                    }
                }
            });

I want to do it inside a public String so that I can reuse it and just get the return value out of it. 我想在一个public String此操作,以便可以重用它并从中获取返回值。 I tried doing it, but the String that I'm using is getting error. 我尝试这样做,但是我正在使用的String出现错误。

Can anyone please give me an example on how to do this properly? 任何人都可以给我一个有关如何正确执行此操作的示例吗?

I don't have enough reputation to comment, but I don't understand why you use String as method name. 我没有足够的声誉来评论,但是我不明白为什么您使用String作为方法名称。 Isn't String in public void String is not allow because String is a reserved keyword and not allow to use as a method name. 是不是在字符串 public void String是不允许,因为String是一个保留关键字,不允许作为方法的名称使用。

If you want the return type as String, you need to declare the method as public String yourMethodName 如果要将返回类型设为String,则需要将方法声明为public String yourMethodName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM