Android Question Format string to HTML Entities

RandomCoder

Well-Known Member
Licensed User
Longtime User
My UPNP Media Browser app is progressing quite nicely now, but I have come across an error caused by sending a HTTP request that is not formatted correctly. The error I receive is "Success=False Error:Bad Request".
I have determined that this is caused by an amplisand in the item I am trying to browse. The code below shows the result of a browse request, the data I extract using an XOM parser and the subsequent browse request...
HTML:
##### BROWSE RESULTS (RAW DATA) #####
<s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <u:BrowseResponse xmlns:u="urn:schemas-upnp-org:service:ContentDirectory:1">
        <Result>
            &lt;DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/"
                          xmlns:dc="http://purl.org/dc/elements/1.1/"
                          xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/"
                          xmlns:dlna="urn:schemas-dlna-org:metadata-1-0/"&gt;
                &lt;container id="musicdb://artists/635/23/?albumartistsonly=true&amp;amp;artistid=635"
                              parentID="musicdb://artists/635/?albumartistsonly=true"
                              restricted="1"
                              searchable="0"&gt;
                    &lt;dc:title&gt;Free All Angels&lt;/dc:title&gt;
                    &lt;dc:creator&gt;Unknown&lt;/dc:creator&gt;
                    &lt;upnp:artist role="Performer"&gt;Ash&lt;/upnp:artist&gt;
                    &lt;upnp:artist role="AlbumArtist"&gt;Ash&lt;/upnp:artist&gt;
                    &lt;upnp:album&gt;Free All Angels&lt;/upnp:album&gt;
                    &lt;upnp:genre&gt;Unknown&lt;/upnp:genre&gt;
                    &lt;upnp:albumArtURI dlna:profileID="JPEG_TN"&gt;http://192.168.0.100:1309/%25/C72C9D9A515FA434C903C17A8E0ACBA6/Folder.JPG&lt;/upnp:albumArtURI&gt;
                    &lt;res protocolInfo="xbmc.org:*:fanart:*"&gt;http://192.168.0.100:1309/%25/032E881D8D061734E77C697FC2E8BBD9/ash-4dd6ac34e27b0.jpg&lt;/res&gt;
                    &lt;upnp:class&gt;object.container.album.musicAlbum&lt;/upnp:class&gt;
                &lt;/container&gt;
            &lt;/DIDL-Lite&gt;
        </Result>
        <NumberReturned>1</NumberReturned>
        <TotalMatches>1</TotalMatches>
        <UpdateID>0</UpdateID>
        </u:BrowseResponse>
    </s:Body>
</s:Envelope

Container-->[id=musicdb://artists/635/23/?albumartistsonly=true&artistid=635]
            [title=Free All Angels]
            [class=object.container.album.musicAlbum]
            [resolution=]
            [location=http://192.168.0.100:1309/%25/032E881D8D061734E77C697FC2E8BBD9/ash-4dd6ac34e27b0.jpg]

##### SEND BROWSE REQUEST #####
<?xml version="1.0" encoding="utf-8"?>
    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
    s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
        <s:Body>
            <u:Browse xmlns:u="urn:schemas-upnp-org:service:ContentDirectory:1">
                <ObjectID>musicdb://artists/635/23/?albumartistsonly=true&artistid=635</ObjectID>
                <BrowseFlag>BrowseDirectChildren</BrowseFlag>
                <Filter>*</Filter>
                <StartingIndex>0</StartingIndex>
                <RequestedCount>100</RequestedCount>
                <SortCriteria></SortCriteria>
            </u:Browse>
        </s:Body>
    </s:Envelope>
BAD REQUEST!
If I modify the ObjectID and replace "&" with "&amp;" which is the correct HTML entity it works fine.
Now for my question... I have tried to use the StringUtils library to encode the ObjectID but that didn't work as the URLEncode function is different to what I require.
Is there a way to format my string such as...
B4X:
Dim su As StringUtils
browseID = su.EncodeUrl(objectID, "UTF8")
This doesn't work and so at pressent I am using...
B4X:
browseID = objectID.Replace("&", "&amp;")
Which works but only covers the amplisand entity, I'd like to cover all eventualities if possible?

Thanks,
RandomCoder
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
Thank you Erel. I searched the forum for "HTML Entities" and found a post that was two years old stating that there was no library or built-in function that currently did this. I'll endeavor to search a little harder next time ;)

Ps. I like you're solution. I would have just used the string.replace function several times but I'm guessing that this would have been way less efficient than you're code as it would have iterated over the entire string each time. Whereas with you're solution it iterates over the string just once. Nice!
B4X:
Public Sub EscapeXml(Raw As String) As String
  Dim sb As StringBuilder
  sb.Initialize
  For i = 0 To Raw.Length - 1
    Dim c As Char = Raw.CharAt(i)
    Select c
      Case QUOTE
        sb.Append("&quot;")
      Case "'"
        sb.Append("&apos;")
      Case "<"
        sb.Append("&lt;")
      Case ">"
        sb.Append("&gt;")
      Case "&"
        sb.Append("&amp;")
      Case Else
        sb.Append(c)
    End Select
  Next
  Return sb.ToString
End Sub

Thanks a lot,
RandomCoder :D
 
Upvote 0
Top