简体   繁体   中英

How to send large data from native Java to JavaScript?

I am developing an Android app, I have a large JSON string around 5 MB in the Java native code. I need to send this string from Java code to JavaScript (JS files are in assets folder). When I run the application, it hangs.

public String readFileDataofIamge()
{
    StringBuilder text = new StringBuilder();
    String FILE_NAME = "ImageDataFile.txt";
    try {

        BufferedReader reader = new BufferedReader(new InputStreamReader(openFileInput(FILE_NAME)));
        while ((line=reader.readLine())!=null) {
            text.append(line.toString());}
        reader.close();
    }
    catch (Exception e) {
    }

    return text.toString();
}

@JavascriptInterface
public String readImageData()
{
    return readFileDataofIamge();
}

JS Code:

function getDatafromFile() {
    var imageData=Android.readImageData();
}

If I return some small string value like hello world , I get the value in JavaScript code.

So is this possible to send this large data from native code to JavaScript code? If not possible is there any other approach to send large data from native code to JS Code?

Condition given: There is no internet connection, so I cannot make any HTTP or network call.

============================================================================

Application overview:

I have an Android app, in which I'm using WebView. all web related files (js and HTML) are inside the assets folder. On first launch I need to get all the data from the server. (I have internet connection at the very first time of app launch) Data is very large, it's around 5 MB, but it may very up to 50 MB. I need to store this data somewhere. so that If I relaunch the app anytime without the internet connection, the app should have this data and it should work in offline mode.

So for this requirement, I have tried to write this data (around 20 MB) into internal storage file and trying to read this file and sending data to JavaScript code. But it's not working.

I have thought to use SQLite DB instead of storing in File, But I think again there will be the same problem while sending data from native code to JS code.

So could you guys please suggest some better approach.

Your problem won't get solved if you store the data in a database unless you won't need the whole file at once.

If the Website uses REST or similar and you have access to the code you could hook the requests and send them over to the Java / Kotlin part of your App. Otherwise you could use WebViews interceptRequest .

Once you are able to get the requests you can return the required data from your database . Imagine it like representing the backend for the website.

Example: Website requires all users via GET users/all . You intercept this request via a hook which calls a Java method and then returns the result OR via interceptRequest. Either way you will have your local database on which you run a query like (pseudocode): Select * from USER . Then return the result.

Since you are dealing with JSON you could use a NOSQL database like CouchbaseLite which directly stores the JSON . Disadvantage: if you need to modify the data you will have to convert it to a POJO and back to JSON once you return it / store it again. Or use a SQLite database. For this you would have to convert the JSON you download to POJOs and then store them.

It would be great if the backend (from where you get your JSON) allows you to get different model types and not everything at once (otherwise you might run into memory issues). You could use Retrofit for networking and Moshi or Gson for your conversion to POJOs .

If there are no REST requests or similar and you need the whole file at once. Honestly I'd say there is no way of handling this in pleasent way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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