Different behavior of WebView

AlpVir

Well-Known Member
Licensed User
Longtime User
I have a normal HTML page FFF.htm that I also moved my web space

<HTML>
<BODY>
<TABLE><TR><TD>
àèìòù
</TD></TR></TABLE>
</BODY>
</HTML>

the lines

B4X:
FileName = "FFF.htm"
h = ReadTextReader (FileName)
WebView1.LoadHtml (h)

displays five strange characters

while the lines

B4X:
FileName = "FFF.htm"
WebView1.LoadUrl ("http://......../" & FileName)

display àèìòù

How To remedy this, I wanted to use the local file?
I should add that I tried to enter several different charset in the HTML page, without success.

charset=iso-8859-1
charset=windows-1252
charset=utf-8
etc etc

I would not be forced to use
&agrave; for à
&egrave; for è
&igrave; for ì
etc. etc.

Thanks for your attention.
 
Last edited:

warwound

Expert
Licensed User
Longtime User
How To remedy this, I wanted to use the local file?
I should add that I tried to enter several different charset in the HTML page, without success.

charset=iso-8859-1
charset=windows-1252
charset=utf-8
etc etc

I would not be forced to use
&agrave; for à
&egrave; for è
&igrave; for ì
etc. etc.

Thanks for your attention.

How have you tried setting the content type in your local HTML file?
Typically you'd use a meta tag like this:

B4X:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

Obviously using the desired charset attribute if UTF-8 isn't how your HTML file has been encoded...

Presumably you are a Windows user so open your HTML file in Notepad.
Choose File menu then Save As.

Notice the Encoding drop down options near the Save button?

Select UTF-8 and save the HTML file.
You can now be sure that it's saved with UTF-8 encoding and use the above meta tag within the (currently non-existant) HEAD section of your HTML:

B4X:
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</HEAD>
<BODY>
<TABLE><TR><TD>
àèìòù
</TD></TR></TABLE>
</BODY>
</HTML>

Martin.
 
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
As mentioned I've used different charset, placing them in the tag <HEAD> <HEAD>


B4X:
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</HEAD>


No results!
I think at this point that this strange behavior stems from a bug
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Is the problem with your webpage or with the way that you're getting the webpage into the WebView?

Try this:

B4X:
FileName = "FFF.htm"
WebView1.LoadUrl ("file:///android_asset/" & FileName)

Does that work?

If so then there is not a problem with the webpage but a problem with the way that you're getting data which is NOT UTF-8 encoded into the WebView.

What does this line do:

B4X:
h = ReadTextReader (FileName)

Is it expecting UTF8 encoded characters but getting characters encoded to the ISO-8859-1 standard?

Martin.
 
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
The lines

B4X:
FileName = "FFF.htm"
WebView1.LoadUrl ("file: / / / android_asset /" & FileName)

give error (file not found).

Here is the sub ReadTextReader

B4X:
Sub ReadTextReader (Nom As String) 
    Dim TextReader1  As TextReader
   Dim sb           As StringBuilder
   Dim line         As String
   Dim h As String 
   Dim i As Int
   Dim S As String 
    sb.Initialize
    TextReader1.Initialize(File.OpenInput(File.DirAssets , Nom))
    line = TextReader1.ReadLine    
    Do While line <> Null
        line = TextReader1.ReadLine
      
      '---- debug
      For i=1 To  line.Length -1
            S= line.CharAt(i-1) 
            Log (S & " " & Asc(S))
      Next 
      '--- end debug 
      
      sb.Append(line).Append(CRLF)
    Loop
    TextReader1.Close
   h=sb.ToString 
   Return  h
End Sub


Looking at the best diving I have shown that the statment :

B4X:
line = TextReader1.ReadLine


If the file FFF.htm contains

omissis
............
aeiouàèìòù
..........
omissis

The code snippet :
B4X:
  '---- debug
      For i=1 To  line.Length -1
            S= line.CharAt(i-1) 
            Log (S & " " & Asc(S))
      Next 
      '--- end debug



produces the following log :

omissis
.............
a 97
e 101
i 105
o 111
u 117
X 65533
X 65533
X 65533
X 65533
X 65533
..............
omissis

where X is a little square on the emulator and

B4X:
àèìòù

on the screen, exactly.


It would appear that the problem was in reading the file.
The question begins to be very convoluted and complicated.
Thank you for the help they are giving me.
 
Last edited:
Upvote 0

warwound

Expert
Licensed User
Longtime User
The code assumes you have added file FFF.htm to your application using the Files tab bottom right.
But note that lower-case filenames are supported - upper-case filenames may cause problems in some (or all?) circumstances.

B4X:
FileName = "FFF.htm"
WebView1.LoadUrl ("file:///android_asset/" & FileName)

Look at the documentation for TextReader.

There is the Initialize method and the Initialize2 method.

Initialize defaults to UTF8 encoding whereas Initialize2 lets you specify the encoding.

So you want to change your code to something like:

B4X:
TextReader1.Initialize2(File.OpenInput(File.DirAssets , Nom), "????")

Now what String to use for ISO-8859-1?

I found this post which suggests you should do:

B4X:
TextReader1.Initialize2(File.OpenInput(File.DirAssets , Nom), "8859-1")

Try that and post again with your results.

Martin.
 
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
Your suggestion to pay attention to the sub ReadTexReader was essential.
We suggest the method has allowed the gods Inizialize2 that solved everything. Thank you very much.
B4X:
TextReader1.Initialize2 (File.OpenInput (File.DirAssets, Nom), "8859-1")

Let me ask you at this point to take a look at my post

http://www.b4x.com/forum/basic4android-updates-questions/14247-webview-skid.html

that always a problem of suboptimal visualization of the same files FFF.htm
Thanks again
 
Upvote 0
Top