¿Reflection?

CapReed

Member
Licensed User
Longtime User
Hello again,

I've been seeing some example about the use of the library to be used directly Reflector Java functions, or so I understand my bad English. I would like to learn more about the subject, for example, to have a simple code in Java to BA4. This code is used to implement that included one encoding to utf8 natively in Java. Could some kind soul put the code necessary to reflector if possible?

Like I'm saying something very rare, as we say in Spain, ignorance is very bold ... :sign0144:

HTML:
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import com.sun.corba.se.impl.ior.ByteBuffer;
import java.nio.*;


public class test {
   
    public static void main(String[] args) throws Exception {
          System.out.println("Gestió");
          CharsetDecoder dec;
          //Charset charset= Charset.forName("ISO-8859-1");
          String var=new String("Gestió");
          byte[] arrByte = var.getBytes("ISO-8859-1");
          
          //ByteBuffer BB = new ByteBuffer();
          
          //Charset charset= Charset.forName("UTF-8");
          //CharsetDecoder decoder=charset.newDecoder();
   
          //decoder.decode(var.getBytes("Gestió"));
          //decoder.
          //System.out.println(URLDecoder.decode("Gestió"));
          String result = new String(arrByte, "UTF-8");
          System.out.println(result);
          System.out.println("FI");
        }

}
 

CapReed

Member
Licensed User
Longtime User
Thanks for the info, Informatix. Now I can only clear the issue.

Erel, could you please tell me what the role similar to the one posted? As mentioned in another post, I have a problem in order to successfully convert the bad string "Gestió"" in a correct string "Gestión" using the knowledge I have. If you could tell me would be great as you would.

Thank you!
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
When I run this EditText1.text = "Björn" shows "Björn" as expected.

Byte = var.GetBytes("ISO-8859-1") produces an array of bytes coded in ISO-8859-1 from the UTF16 formatted string in var.

result = BytesToString(arrByte, 0, arrByte.Length, "UTF8" is incorrect as it is trying to produce a UTF16 formatted string from arrByte assuming arrByte is coded in UTF8, but it is not as you haved coded it in in ISO-8859-1.

To round trip from UTF16 to ISO-8859-1 and back you do.

arrByte = var.GetBytes("ISO-8859-1")
result = BytesToString(arrByte, 0, arrByte.Length, "ISO-8859-1")

Remember that all strings within Jave are coded as UTF16.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
arrByte = Var.GetBytes("CP1250")
Result=BytesToString(arrByte, 0, arrByte.Length, "ISO-8859-1")
Wrong for the reason I already gave. I think you have a misunderstanding about how encodings work. I would expect that a WebView would take care of the encoding when showing a web page. What exactly are you trying to do?

Is "ISO-8859-1" the correct format for EditText boxes in B4A or is it "UTF8?"
Neither, as I said in my post all strings in Basic4android/Java are encoded as UTF16 but you normally don't need to wory about that - only about the encoding of anything coming from an external source.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
B4X:
   Ttext1 = File.ReadString(File.DirAssets, "test1.txt")
The first failure is here. ReadString is assuming a UTF8 encoded file whereas it is actually a single byte encoding. The black triangles are bytes values that are not valid as UTF8 codepoints. You need to read the file with the correct encoding.
B4X:
   Dim Istream As InputStream
   Istream = File.OpenInput(File.DirAssets, "test1.txt")
   Dim Treader As TextReader
   Treader.Initialize2(Istream, "ISO-8859-1")
   Ttext1 = Treader.ReadAll
   Istream.Close   
   EditText1.text = Ttext1
The byte data in the file has now been read into a Java UTF16 string assuming that it represents string data encoded in a single byte character set. You do not need to worry about encodings in your program once you have read the data correctly.

I don't understand what you think these code blocks might be accomplishing. Read the comments in my oringinal answer to see what is happening here.
B4X:
   var = Ttext1
   arrByte = var.GetBytes("ISO-8859-1")
   result = BytesToString(arrByte, 0, arrByte.Length, "ASCII")
   EditText2.text = result
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Where does your file come from ?
How did you ceate it ?
I suspect that you created it on the PC with a text editior.
The file is encoded with "windows-1252" or "ISO-8859-1".
You must define the encoding when you read the file.
The code below works:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim Ttext1 As String
    
    Activity.LoadLayout("textconv1")     'Default, just in case; Altijd doen, ook na orientatie change!

    Dim tr As TextReader
    tr.Initialize2(File.OpenInput(File.DirAssets, "test1.txt"),"windows-1252")
    Ttext1 = tr.ReadAll
    EditText1.text = Ttext1
End Sub
Best regards.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
If you had saved your file in the NotePad with UTF-8 encoding you wouldn't have had the problem.
The default encoding in NotePad is not ASCII but ANSI (windows-1252).
If you want to write files on the PC for use in Android I'd suggest you NotePad++.

I have no experience in httputils2.

Best regards.
 
Upvote 0
Top