Android Question StringUtils EncodeURL problem

Alex_197

Well-Known Member
Licensed User
Longtime User
Hi all.

I'm using StringUtils EncodeURL both in B4A and B4I.

It returns me different result for encoding the same string

The same code for both b4a and b4i

B4X:
Sub Button1_Click
   
    Try
       
        Dim str As String,strEnc As String
        Dim su As StringUtils
      
        str="12 - 1.jpg"
        strEnc=su.EncodeUrl(str,"UTF-8")
        Log(str & TAB & strEnc)
     
   
    Catch
        Log("Button1_Click " & Me &  LastException)      
    End Try
       
End Sub

B4A returns -
12 - 1.jpg 12+-+1.jpg
B4I returns -
12 - 1.jpg 12%20-%201.jpg

The problem is that I'm getting 404 error when I'm trying to download the image by using B4A encoded string but it's Ok for B4I

Why the same code gives me different results?

Thanks
 

Alex_197

Well-Known Member
Licensed User
Longtime User
Please note that the B4A/B4J and B4i's handling of encoding spaces and decoding plus signs stems from the underlying libraries that are used to handle the encoding/decoding. Also note that at one time (and it still may be true) that B4i does not decode a plus sign to a space. For a more nuanced explanation and links that back up the explanation, please see https://www.b4x.com/android/forum/threads/stringsutil-utf-8-encoding-decoding.91321/post-579360
But b4i encodes properly by replacing spaces with '%20' when b4a does it by replacing spaces with '+'.
If I would send a PostString it won't be a problem at all because ASP.NET page that handles this post request will decode it properly.
But in my case I don't use PostString but j.Download(URL) and I'm downloading an image directly to the app. Of course the url http://71.251.18.254:81/TFC/WebDocs/12+-+1.jpg returns 404 error.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
But b4i encodes properly by replacing spaces with '%20' when b4a does it by replacing spaces with '+'.
Did you read the link I provided? I'm not disputing that there is a difference, I'm trying to provide the underlying reasons for the difference. In B4A/B4J, UrlEncode encodes the data as if is used after the ? of a Url. In that situation, the + works. It is in the case you are using UrlEncoding for that the + does not work. In this case, the solution is to replace the + with a %20 as mentioned. Also note, that B4i does not change a + to a space (unless that was changed) when decoding, even though + means space in portions of the Url (after the ?). Neither (B4A/B4J and B4i) are correct/or incorrect on all counts, just in certain usage domains as pertaining to Url(s).
 
Upvote 0
Top