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
try
B4X:
strEnc=su.EncodeUrl(str,"UTF8")
Didn't work either
12 - 1.jpg 12+-+1.jpg


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

Alex_197

Well-Known Member
Licensed User
Longtime User
Yes I noticed it too. However, can you post your download sub?
B4X:
                Photo=su.EncodeUrl(Photo,"UTF-8")
                
                URL= modFun.URLImage & Photo
        
                j.Initialize("", Me)
        
                j.Download(URL)
        
                Wait For (j) JobDone(j As HttpJob)
        
                If j.Success Then
            
                    Dim out As OutputStream = File.OpenOutput(Starter.FileDir, Photo, False)
                    File.Copy2(j.GetInputStream, out)
                    out.Close '<------ very important
                    Log("Photo " & Photo & " downloaded Ok")
                    Main.TestRet=True
                Else
                
                    modFun.ShowError("clsRefreshResume_HTTP Download error " &  CRLF & "Try again later with better Internet connection and check if photo exists on a provider profile.~<br/>URL=" & URL)
                    Main.TestRet=False
                End If
        
                j.Release
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
no need to encode. okhttputils2 will do it automatically
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
Can you try using download2 and see if it works ok?
I don't have any parameters so there is no reason to use download2. The only difference between download vs download2 is parameters array.

The problem is in su.EncodeURL in b4A - it replaces spaces with '+' instead of '%20 '. If I would use PostString method it would be Ok because server will encode it properly but I'm downgliding image directly and in this case image name should be either properly encoded or encode shouldn't be use at all.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Oh,
I don't have any parameters so there is no reason to use download2. The only difference between download vs download2 is parameters array.

The problem is in su.EncodeURL in b4A - it replaces spaces with '+' instead of '%20 '. If I would use PostString method it would be Ok because server will encode it properly but I'm downgliding image directly and in this case image name should be either properly encoded or encode shouldn't be use at all.
Thought that your image string was a parameter.
Ok then, why don't you just simply replace the +'s with %20's and get a fix this way? If it's a bug, it should be treated as such. In the mean time just do the replacement after the encoding process. By the way, + is a valid encoding output for parameters. For urls indeed you should use %20's though it's not a good practice.
 
Upvote 0

mcqueccu

Well-Known Member
Licensed User
Longtime User
In B4A replace the + with %20

B4X:
encodedUrl = encodedUrl.Replace("+", "%20")

 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
In B4A replace the + with %20

B4X:
encodedUrl = encodedUrl.Replace("+", "%20")

Here is an actual url to the image Working
Here is a url to the same image where we are using '+' instead of '%20' Not working
 
Upvote 0

mcqueccu

Well-Known Member
Licensed User
Longtime User
Here is a url to the same image where we are using '+' instead of '%20' Not working


I dont understand why you are sticking to the +. Use the code I posted to replace the pluses to %20
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")
        strEnc = strEnc.Replace("+", "%20") '<-----Add this in B4A only to remove the +
        Log(str & TAB & strEnc)
    
  
    Catch
        Log("Button1_Click " & Me &  LastException)     
    End Try
      
End Sub
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
I can use this (without URL encoding) without any problems (local Apache server).

1620109575894.png


B4X:
Sub DLFile
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("http://192.168.178.23/headers/12 - 1.jpg")
    
    Wait For (j) JobDone(j As HttpJob)
        
    If j.Success Then
        Dim out As OutputStream = File.OpenOutput(File.DirApp,"12 - 1.jpg",False)
        File.Copy2(j.GetInputStream, out)
        out.Close
    End If
        
    j.Release
End Sub
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Why the same code gives me different results?

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
 
Upvote 0
Top