Passing a JSONArray to a webview using addJavascriptInterface can provide valuable data

Within my Android app assets, I have stored an HTML page that I want to display using a WebView and add parameters to the webpage.

To achieve this, I populated all the values in a JSONArray and utilized 'addJavascriptInterface' to incorporate this JSONArray into the webpage's JavaScript.

However, when attempting to access the value in JavaScript, it appears as [object Object] with an undefined length...

JSONArray json = buildJSONArray();

webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);

webView.addJavascriptInterface(json, "data");
webView.loadUrl("file:///android_asset/test.html");

I tried replacing the JSONArray with a simple String, but encountered the same issue...

How can I effectively send a JavaScript parameter to my HTML page from Android? It seems like there is something missing in the implementation of the 'addJavascriptInterface' method...

Any help or guidance on this matter would be greatly appreciated :)

Answer №1

How to Convert Json into a String in Android

Create a new JsonObject and add a property called "name" with the value of "gokul":
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", "gokul");

Convert the JsonObject into a string:
String json = jsonObject.toString();

Return the json string:
return json;

JavaScript: Parsing a JSON String

Parse the json string using the JSON.parse method:
var json = JSON.parse(json);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieving data from a JSON using Typescript and Angular 2

Here is an example of what my JSON data structure looks like: { "reportSections": [ { "name": "...", "display": true, "nav": false, "reportGroups": { "reports": [ { "name": "...", "ur ...

Is there a regular expression that can identify whether a string is included in a numbered list?

Struggling with creating a regular expression to determine if a string is part of a numbered list like those in word processors. Need it to return true only if the string starts with a number, followed by a full stop and a space. Easy for single or doubl ...

Tips for maintaining the browser scroll bar at the top when switching routes in AngularJS

How can I ensure that the scrollbar is always at the top when a user is redirected to a different page after scrolling down on the home page? The autoscroll feature in the code below doesn't seem to be working. Any suggestions would be greatly appreci ...

Is it possible to incorporate Vue and Vuetify into an existing project that requires IE compatibility?

Currently in the process of enhancing a legacy project with new functionality. The front end is currently relying solely on jQuery for all the webpages. I have been tasked with adding another webpage and would like to incorporate Vuetify + Vue due to the i ...

Is there a way to prevent an external script from making changes to an inline style?

There is a mysterious script running on a page that seems to be controlling the height of an inline style. The source of this script modifying the height property is unknown. <div class="vgca-iframe-wrapper wpfa-initialized" style="heigh ...

What sets onEnter apart from onStart in ui-router?

I am transitioning to the latest version of ui-router (1.0.0-alpha.5) and I am exploring how to utilize the onEnter hook versus the onStart hook: $transitions.onStart() as well as $transitions.onEnter() In previous versions, we only had the event $sta ...

I am interested in utilizing angular 4 ng2-ui/map with places-auto-complete functionality that is restricted to a specific country

Check out my code snippet below: <input class="form-control" placeholder="Pickup Location" places-auto-complete (place_changed)="pickupChanged($event)" formControlName="pickup_location" [types]="['geocode']" /> I am trying to figure out ...

Creating compressed files using JavaScript

I am currently working on unzipping a file located in the directory "./Data/Engine/modules/xnc.zip" to the destination folder "./Data/Engine/modules/xnc". Once I have completed writing to these files, I will need an easy method to rezip them! While I wou ...

I keep encountering an Uncaught TypeError when trying to read the property 'options' of null, despite having the element ID properly defined

I am a newcomer to the world of Javascript and HTML. Despite having the element defined in HTML, I am encountering an exception. Could someone please offer assistance? My goal is to create a shape (initially a circle) based on user input such as shape type ...

`How does the RecyclerView.Adapter function when extended?`

Explanation Needed: Extending RecyclerView.Adapter I'm struggling to fully grasp a certain piece of code. Could someone help me understand when the method is extended and its purpose? Here's an example of the code in question: public class Com ...

Turn off the extra space inserted by DataTables

Help needed with centering table header text. <table class="table table-bordered table-hover center-all" id="dataTable"> <thead> <tr> <th scope="col" style="text-align: center">Nam ...

Tips for effectively handling errors in a RESTful API

Have you seen this project on Github? It's a RESTful API designed for managing a movie rental service. While it currently "works", one issue is that error messages are sent directly to clients from internal methods. Consider this code snippet: /* GE ...

What is the proper way to implement the post method in Android's MVC pattern?

As someone who is new to integrating the MVC pattern with JSON post method on Android, I am looking for guidance on how to successfully post the provided data using JSON to the URL . Can someone assist me with implementing the MVC pattern in Android? ME ...

I'm attempting to produce the JSON string that is being returned from PHP, but I'm struggling to make it happen

Here is a JSON string returned from PHP. Can you suggest how to retrieve values from it? [ { "picid": "13", "itemcode": "P-0000001", "filename": "-1970-01-011.jpg", "primarystatus": "active" }, { "picid" ...

Using PHP to retrieve data from a JSON file (obtaining blank results containing numerical values)

I'm attempting to extract data from a JSON file using PHP I am interested in retrieving all URLs (links), http://www.google.com http://www.bing.com JSON This is what I have attempted so far PHP Appreciate any help ...

When attempting to display an identical image on two distinct canvas elements in Angular 6

I am facing an issue where the image is only rendered on the second canvas instead of both canvases. I need assistance in finding a solution to render the same image on both canvases. Below is a screenshot demonstrating that the image is currently only re ...

Sending data to SOAP API using AngularJS

I've been struggling to send data to a SOAP API but have hit a roadblock. I've attempted various methods but continue to encounter errors when making the API call. The API URL is - and it requires data in XML format like <?xml version="1.0" ...

Tool to insert content into the initial subdirectory

My goal is to develop a bookmarklet that can add text after the main domain but before any subpath. For example: http://example.com/home/start -> http://example.com/text/home/start I am considering storing the full path, removing the domain, replacing ...

Having trouble with Angular ngRoute functionality?

I'm currently working on configuring a basic Angular app. Here is my main HTML: <html ng-app="CostumerApp"> <head> <title> Customers </title> <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstr ...

alter POST information after transmitting it to PHP from PYTHON

I have a server generating JSON data every few minutes internally. To manipulate this data and present it on a web interface, I need to pass it to an external server. Below is the Python script I use to send the data to a PHP script: x = json.dumps(data) ...