Android Question [Solved] Job.GetString to List

asales

Expert
Licensed User
Longtime User
I get a string response in this format (lines):
B4X:
1. The Godfather 
2. Citizen Kane 
3. Casablanca 
4. Schindler's List 
5. Pulp Fiction 
6. The Shawshank Redemption 
7. 2001: A Space Odyssey 
8. The Dark Knight 
9. Fight Club 
10. Forrest Gump
I search for several codes in the forum, but I only found this way (save the string to file and read to a list)
B4X:
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
    Log(j.GetString)
    File.WriteString(xui.DefaultFolder, "1.txt", j.GetString)
    lstResponse = File.ReadList(xui.DefaultFolder, "1.txt")
End If
Is possible to generate a list from this string response without save and read the file?

Thanks in advance for any tips .
 
Solution
i don't fully agree with the selections on the list, but i would use regex.split() and list.addall().
i'm guessing the string uses crlf (or possibly "\r\n", depending).
so split the string on crlf to get an array of strings and then create a list and
call addall.

dim result as string = job.getstring
dim films() as string = regex.split(crlf, result)
dim filmlist as list
filmlist.addall( films )

or save the string and read back in as a list...

drgottjr

Expert
Licensed User
Longtime User
i don't fully agree with the selections on the list, but i would use regex.split() and list.addall().
i'm guessing the string uses crlf (or possibly "\r\n", depending).
so split the string on crlf to get an array of strings and then create a list and
call addall.

dim result as string = job.getstring
dim films() as string = regex.split(crlf, result)
dim filmlist as list
filmlist.addall( films )

or save the string and read back in as a list...
 
Upvote 1
Solution

MikeH

Well-Known Member
Licensed User
Longtime User
I search for several codes in the forum, but I only found this way (save the string to file and read to a list)
B4X:
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
    Log(j.GetString)
    File.WriteString(xui.DefaultFolder, "1.txt", j.GetString)
    lstResponse = File.ReadList(xui.DefaultFolder, "1.txt")
End If
Is possible to generate a list from this string response without save and read the file?

Thanks in advance for any tips .

Your method is the same as mine. I think what would be ideal is:

B4X:
j.GetList
 
Upvote 0

asales

Expert
Licensed User
Longtime User
B4X:
j.GetList
This is a httpjob?
I don't found this method:
1725038269981.png
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
?
two direct options

B4X:
    Dim Text As String = $"1. The Godfather
2. Citizen Kane
3. Casablanca
4. Schindler's List
5. Pulp Fiction
6. The Shawshank Redemption
7. 2001: A Space Odyssey
8. The Dark Knight
9. Fight Club
10. Forrest Gump"$
    
    Log("** Use Array **")
    Dim OutArray() As String = Regex.Split("./",Text)
    For Each s As String In OutArray
        Log(s)
    Next
    Log("--------------------")
    Log("** Use List **")
    Dim OutList As List = Regex.Split("./",Text)
    For Each s As String In OutList
        Log(s)
    Next

1725040413118.png
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Another option is to change the pattern for the end of line, carriage return, or newline.
B4X:
Dim OutArray() As String = Regex.Split("\r?\n|\r",Text)
B4X:
    Dim OutList As List = Regex.Split("\r?\n|\r",Text)

Note: CRLF is only newline
1725041108008.png
 
Last edited:
Upvote 0
Top