iOS Question Error decoding data as string reading data from a Download from a HttpJob -- solved

sdesan

Member
Licensed User
Longtime User
Probably the answer is very simple but i can't read data from an httpjob when the results contains special char as ò à è ù ecc.
Sure is a decode problem but i can't find right syntax for solve my problem
thia is my code
B4X:
    Dim j As HttpJob
    j.Initialize("j", Me)
    j.Download("https://mysever.est/filmtitlelist.php")
    Wait For (j) JobDone(j As HttpJob)
    File.WriteString(File.DirDocuments,"localfile.txt",j.GetString)

web server returns a list of movie titles like this

HTML:
001 La vita è bella
002 Roma città aperta
003 La dolce vita
004 Paisà
005 Sciuscià

Anda httpjob reports this error

Error occurred on line: xxx (HttpJob)
Error decoding data as string.


and is related to
B4X:
File.WriteString(File.DirDocuments,"localfile.txt",j.GetString)
i have also tried
B4X:
File.WriteString2(File.DirDocuments,"localfile.txt",j.GetString,"UTF8")
with same error.
the problem is clearly linked to the presence of the characters ò à ù è but i can't find a solution

In B4A i use
B4X:
Dim out As OutputStream = File.OpenOutput(File.DirInternal, "localfile.txt", False)
File.Copy2(job.GetInputStream, out)
out.Close
and have no error and accented characters are showed as soft hyphen �

Thank you
 
Last edited:

TILogistic

Expert
Licensed User
Longtime User
?
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
If you want, you can check the encoding of the response first:

check:
B4X:
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Log(j.Response.ContentType)
Sample: UTF-8
1640848805847.png


or with

Postman
 
Upvote 0

sdesan

Member
Licensed User
Longtime User
This error means that the remote file encoding is not UTF8. You need to use Job.GetString2 with the correct encoding.

If you just want to save the data then follow the link in the above post.
Thank you Erel,
using code in the above post i can write data locally but when i try to read local text file
B4X:
mylist=File.ReadList(File.DirDocuments,"localfile.txt")

i have again an encoding problem

Error reading file. Incorrect encoding

Now i can solve it simply by editing the php file on the server and convert all accented char or create a little function in B4i for filtering web result, but i'll like to understand better difference from B4A and B4i then often are quite important

in B4A i use a TextReader for read downloaded result

B4X:
Dim j As HttpJob
j.Initialize("j", Me)
j.Download("https://myserver.est/filmtitlelist.php)   
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
    Dim out As OutputStream = File.OpenOutput(File.DirInternal, "localfile.txt", False)
    File.Copy2(job.GetInputStream, out)
    out.Close
End If
j.release
Dim reader As TextReader
reader.Initialize(File.OpenInput(File.DirInternal, "localfile.txt"))
line = reader.ReadLine

in B4i i have to use a List (if is correct)

B4X:
Dim mylist As list
Dim j As HttpJob
j.Initialize("", Me)
j.Download("https://myserver.est/filmtitlelist.php")
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
    Dim out As OutputStream = File.OpenOutput(File.DirDocuments, "localfile.txt", False)
    File.Copy2(j.GetInputStream, out)
    out.Close
End If
j.Release
mylist=File.ReadList(File.DirDocuments,"localfile.txt")

code is very similar: where is the problem of decoding? in j.download of B4i vs j.download of B4A or in list vs textreader (or in my mind:eek:) ?
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
You can upload an example of the file or how you generate the file in php.

If I'm not mistaken, you are trying to parse an html file.
 
Upvote 0

sdesan

Member
Licensed User
Longtime User
The error is very clear and simple. The text file is not UTF8 encoded. It also a mistake in B4A, but the Android decoder is more lenient.

You need to find the correct encoding and use it. Upload the text file if you don't find it.
Thanks to Oparra.
Correct decoding is ISO-8859-1


B4X:
        j.Initialize("", Me)
        j.Download(URL)
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            ResultURL = j.GetString2("ISO-8859-1")
            File.WriteString(xui.DefaultFolder, "demo.txt", ResultURL)
            Dim List1 As List = File.ReadList(xui.DefaultFolder, "demo.txt")
            For Each s As String In List1
                Log(s)
            Next
        End If

This code by Oparra solved the problem
Thank you Oparra and Erel
 
Upvote 0
Top