Android Question Read from localStorage

tomoshea

Member
Licensed User
Longtime User
HTML:
<!DOCTYPE html>
<html>
  <head>
    <title>reading file</title>
    <meta charset="UTF-8">
    <script type="text/javascript" src="/SD_WLAN/js/jquery.js"></script>
    <script type="text/javascript" src="/SD_WLAN/js/modernizr.custom.14927.js"></script>
    <script type="text/javascript">
    function getFileFromServer(url, doneCallback) {
            var xhr;
            xhr = new XMLHttpRequest();
            xhr.onreadystatechange = handleStateChange;
            xhr.open("GET", url, true);
            xhr.send();
            function handleStateChange() {
                    if (xhr.readyState === 4) {
                            doneCallback(xhr.status == 200 ? xhr.responseText : null);
                    }
            }
    }
    function WriteToFile(that) {
        if (Modernizr.localstorage) {
            // window.localStorage is available!
            localStorage["last"] = JSON.stringify(that);
            //document.getElementById ('main1').innerHTML= localStorage["last"];
        } else {
            // no native support for HTML5 storage :(
            // maybe try dojox.storage or a third-party solution
        } 
    } 
    </script>
</head>
<body>
    <div id="main"></div>
    <div id="main1"></div>
    <div id="file"></div>
    <script type="text/javascript">
          getFileFromServer("http://flashair/sd_wlan/log1.csv", function(text) {
                if (text === null) {
                        // An error occurred
                        document.getElementById ('file').innerHTML="http://flashair/sd_wlan/log1.csv";
                }
                else {
                        // `text` is the file text
                        var lines = text.split('\n');
                        var size = lines.length;
                        document.getElementById ('main').innerHTML= lines[size-2];
                        WriteToFile(lines[size-2]);
                        document.getElementById ('file').innerHTML="http://flashair/sd_wlan/log1.csv";
                }
        });
    </script>

</body>
</html>
 

tomoshea

Member
Licensed User
Longtime User
Hi,
I have written some HTML/Javascript see #1 above which writes some date to localStorage (function WriteToFile(that)).
How do I access this data from my B4A app?
Thank you,
Tom
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Why you ask THE SAME QUESTION IN A NEW THREAD? You already asked it here!


Anyway; i think the webview does not have a html5-datastore. Or i did not know about.
I dont think at all that you should be able to get writeaccess from javascript on any phone. Where is the security?

I would suggest using webview-extra and inject javascript to send data from page to the app the webview is runing in.
Search for webviewextras examples... @warwound have created some really good thread-posts here in forum about that...
 
Last edited:
Upvote 0

tomoshea

Member
Licensed User
Longtime User
Hi Manfred,
I just thought that it should be in a new THREAD.
I know that you can not get writeaccess from javascript and I cannot use php so the above if the webview had the datastore would have given me a solution. I will try your suggestion and post the result.
Thanks again for all your help.
Regards,
Tom
 
Upvote 0

tomoshea

Member
Licensed User
Longtime User
Hi Manfred,
I used the webviewextras lib as suggested. Works fine. I am attaching the relevant code.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim flashair_url As String = "http://flashair/SD_WLAN/rwlast3.htm"
    Dim now As Long
    DateTime.TimeFormat = "HH:mm:ss:SS"
    Dim js As String = "B4A.CallSub('ProcessLastLine',true,document.getElementById('main').innerHTML)"
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim WebView1 As WebView
    Dim wve As WebViewExtras
    Dim pws As PhoneWakeState
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
    Activity.Title = "Flash Air WIFI SD Card - V3"
    pws.KeepAlive(True)
    wve.addJavascriptInterface(WebView1,"B4A")
    wve.addWebChromeClient(WebView1,"wve")
    now = DateTime.now
    Log(DateTime.time(now))   
    WebView1.LoadUrl(flashair_url)
End Sub

Sub Activity_Resume

End Sub


Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'return true if you want to consume the event
    If KeyCode = KeyCodes.KEYCODE_BACK Then
      pws.ReleaseKeepAlive
      Activity.Finish
      ExitApplication
      Return True
      Else
        Return False
    End If
End Sub


Sub WebView1_PageFinished (Url As String)
    now = DateTime.now
    Log(DateTime.time(now))   
    wve.executeJavascript(WebView1,js)
End Sub

Sub WebView1_OverrideUrl (Url As String) As Boolean
   
End Sub

Sub ProcessLastLine(html As String)
    Log(html)
    Dim flds() As String
    flds = Regex.Split(",",html)
End Sub

rwlast3.htm

HTML:
<!DOCTYPE html>
<html>
  <head>
    <title>reading file</title>
    <script type="text/javascript">
    function getFileFromServer(url, doneCallback) {
            var xhr;
            xhr = new XMLHttpRequest();
            xhr.onreadystatechange = handleStateChange;
            xhr.open("GET", url, true);
            xhr.send();
            function handleStateChange() {
                    if (xhr.readyState === 4) {
                            doneCallback(xhr.status == 200 ? xhr.responseText : null);
                    }
            }
    }
    </script>
</head>
<body>
          <script type="text/javascript">
          getFileFromServer("http://flashair/sd_wlan/log1.csv", function(text) {
                if (text === null) {
                        // An error occurred
                        document.getElementById ('file').innerHTML="http://flashair/sd_wlan/log1.csv";
                }
                else {
                        // `text` is the file text
                        var lines = text.split('\n');
                        var size = lines.length;
                        document.getElementById ('main').innerHTML= lines[size-2];
                        //document.getElementById ('file').innerHTML="http://flashair/sd_wlan/log1.csv";
                }
        });
        </script>
    <div id="main"></div>
    <div id="file"></div>
  </body>
</html>

Thanks again,
Tom
 
Upvote 0

tomoshea

Member
Licensed User
Longtime User
Hi Manfred,
Is there any way that I can optimise the Javascript/B4A code to improve performance?
Thanks,
Tom
 
Upvote 0
Top