Android Question RSS - XML parser from dynamic page

GERSON PINTO

Member
Licensed User
Hello guys
I am trying to make a feed reader based on the XmlSax library and sample but I am having problems with parse...
In this case I need download and save the page with xml.
There is an error in parse and I couldn't solve it....
Can anyone give a help?
follow my 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 parser As SaxParser
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 Listview1 As ListView
' 
    Dim Title, Link As String
  

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("webview")
    If FirstTime Then
        parser.Initialize
    End If
    Listview1.Initialize("ListView1")
    Activity.AddView(Listview1, 0, 0, 100%x, 100%y)
    Listview1.SingleLineLayout.ItemHeight = 60dip
  
    download_page
  
    'parse the xml file
    Dim in As InputStream
    in = File.OpenInput(File.DirInternal,"stj.xml")
    parser.Parse(in,"Parse")
    in.Close
 

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub download_page
  
    Link="https://res.stj.jus.br/hrestp-c-portalp/RSS.xml"
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim out As OutputStream=File.OpenOutput(File.DirInternal,"stj.xml",False)
        File.Copy2(j.GetInputStream,out)
        out.Close
              
    End If
    j.Release
End Sub



Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes)

End Sub

Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
    If parser.Parents.IndexOf("item") > -1 Then
        If Name = "title" Then
            Title = Text.ToString
        Else If Name = "link" Then
            Link = Text.ToString
        End If
    End If
    If Name = "item" Then
        Listview1.AddSingleLine2(Title, Link) 'add the title as the text and the link as the value
    End If
End Sub
 
Last edited:

JordiCP

Expert
Licensed User
Longtime User
The 'Wait for' in downloadPage Sub, returns control to the calling Sub.
At that point, your code is processing the output file ("stj.xml") --> it should wait for the called Sub to finish (and check if it finished succesfully or not).

Code should be modified (untested, there may be other issues)

Activity_Create
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("webview")
    If FirstTime Then
        parser.Initialize
    End If
    Listview1.Initialize("ListView1")
    Activity.AddView(Listview1, 0, 0, 100%x, 100%y)
    Listview1.SingleLineLayout.ItemHeight = 60dip
  
    Wait for (download_page) Complete(res as Boolean)   '<--- don't continue until finished
  
    if res Then   '<-- only look for file if file was saved
      'parse the xml file
      Dim in As InputStream
      in = File.OpenInput(File.DirInternal,"stj.xml")
      parser.Parse(in,"Parse")
      in.Close
    Else
      Log("Could not load file")
    End if

End Sub

And download_Page
B4X:
Sub download_page as ResumableSub    '<-- 
    
    Link="https://res.stj.jus.br/hrestp-c-portalp/RSS.xml"
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    Wait For (j) JobDone(j As HttpJob)
    
    Dim res as Boolean = False
    If j.Success Then
        Dim out As OutputStream=File.OpenOutput(File.DirInternal,"stj.xml",False)
        File.Copy2(j.GetInputStream,out)
        out.Close
        res = True      
    End If
    j.Release
    return res   '<-- ResumableSubs return values if we must wait for them to finsih.
End Sub
 
Upvote 0

GERSON PINTO

Member
Licensed User
The 'Wait for' in downloadPage Sub, returns control to the calling Sub.
At that point, your code is processing the output file ("stj.xml") --> it should wait for the called Sub to finish (and check if it finished succesfully or not).

Code should be modified (untested, there may be other issues)

Activity_Create
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("webview")
    If FirstTime Then
        parser.Initialize
    End If
    Listview1.Initialize("ListView1")
    Activity.AddView(Listview1, 0, 0, 100%x, 100%y)
    Listview1.SingleLineLayout.ItemHeight = 60dip
 
    Wait for (download_page) Complete(res as Boolean)   '<--- don't continue until finished
 
    if res Then   '<-- only look for file if file was saved
      'parse the xml file
      Dim in As InputStream
      in = File.OpenInput(File.DirInternal,"stj.xml")
      parser.Parse(in,"Parse")
      in.Close
    Else
      Log("Could not load file")
    End if

End Sub

And download_Page
B4X:
Sub download_page as ResumableSub    '<--
   
    Link="https://res.stj.jus.br/hrestp-c-portalp/RSS.xml"
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    Wait For (j) JobDone(j As HttpJob)
   
    Dim res as Boolean = False
    If j.Success Then
        Dim out As OutputStream=File.OpenOutput(File.DirInternal,"stj.xml",False)
        File.Copy2(j.GetInputStream,out)
        out.Close
        res = True     
    End If
    j.Release
    return res   '<-- ResumableSubs return values if we must wait for them to finsih.
End Sub

well it's not reporting an error but it's not loading listview either
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
I think here'st also a mistake in the parser event prefix.
B4X:
'parser.Parse(in,"Parse")  '<-- wrong
parser.Parse(in,"Parser")
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
The problem is related to encoding. For this link, change the download_page Sub to

B4X:
Sub download_page As ResumableSub  
  
   'Link = "https://res.stj.jus.br/hrestp-c-portalp/RSS.xml"
    Link="http://www.stf.jus.br/portal/RSS/noticiaRss.asp?codigo=1"
  
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    Wait For (j) JobDone(j As HttpJob)
  
    Dim res As Boolean = False
    If j.Success Then
        ' Dim str As String = j.GetString   '<-- Valid for the .xml link. UTF-8 encoding
        Dim str As String = j.GetString2("ISO-8859-1")   '<-- Valid for the .asp link. ISO-8859-1 encoding
        Log(str)  '<-- you can see the encoding used at the beginning of the text.
        File.WriteString(File.DirInternal,"stj.xml",str)

        'Dim out As OutputStream=File.OpenOutput(File.DirInternal,"stj.xml",False)
        'File.Copy2(j.GetInputStream,out)
        'out.Close
        res = True
    End If
    j.Release
    Return res   '<-- ResumableSubs return values if we must wait for them to finsih.
End Sub

To sum up, you should retrieve the content according to the encoding used.


For future questions, please provide more info (error). It will be easier for others to help without the need to compile code. ;)
 
Upvote 0

GERSON PINTO

Member
Licensed User
The problem is related to encoding. For this link, change the download_page Sub to

B4X:
Sub download_page As ResumableSub 
 
   'Link = "https://res.stj.jus.br/hrestp-c-portalp/RSS.xml"
    Link="http://www.stf.jus.br/portal/RSS/noticiaRss.asp?codigo=1"
 
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    Wait For (j) JobDone(j As HttpJob)
 
    Dim res As Boolean = False
    If j.Success Then
        ' Dim str As String = j.GetString   '<-- Valid for the .xml link. UTF-8 encoding
        Dim str As String = j.GetString2("ISO-8859-1")   '<-- Valid for the .asp link. ISO-8859-1 encoding
        Log(str)  '<-- you can see the encoding used at the beginning of the text.
        File.WriteString(File.DirInternal,"stj.xml",str)

        'Dim out As OutputStream=File.OpenOutput(File.DirInternal,"stj.xml",False)
        'File.Copy2(j.GetInputStream,out)
        'out.Close
        res = True
    End If
    j.Release
    Return res   '<-- ResumableSubs return values if we must wait for them to finsih.
End Sub

To sum up, you should retrieve the content according to the encoding used.


For future questions, please provide more info (error). It will be easier for others to help without the need to compile code. ;)

Works fine!
Thank you very much LordiCP!
Without your help I could not do...
 
Upvote 0
Top