Android WebView interactive passing JSON string and parsing method

preface

We all know that data values of basic data types can be passed in WebView interaction, such as int and string

But WebView can also transfer a very important and common data format - JSON data

Passing JSON is the same as passing string data. The key is that the HTML5 side parses the JSON data through the eval () function

One of the most important functions called by the Android side during the transfer process is:

addJavascriptInterface(new MyObject(this,”dd”),”my”); This function has two parameters. The first parameter is passed in which the method in JavaScript is implemented, and the @ javascriptinterface annotation is used to identify whether the method is implemented JS@R_ 502_ 1967 @ Yes, the second parameter is a bridge string, which can be defined at will, as long as the Android side and JavaScript side are consistent

A complete demo of passing JSON data is attached below

index.html

MainActivity.java

activity_ main.xml

MyObject.java

Person.java

The screenshot of operation effect is as follows

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>