B4J Question DecodeUrl error if there are % in the data

pmt

Member
Licensed User
Hi everyone,

I use B4J 5.9 and I have a problem about decodeurl.

I get data from the client application.
B4X:
Dim post_data() As Byte = Bit.InputStreamToBytes(in)
        Dim text_data As String = BytesToString(post_data, 0, data.Length, "UTF8") '

If text_data have % for example : 10% Off

And I use DecodeUrl to decode the text_data.

B4X:
Dim su As StringUtils
        Log("text_data:"&su.DecodeUrl(text_data,"utf8"))

I get error :
B4X:
(IllegalArgumentException) java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: " O"

How to fix this error ?

Regards,
PMT.
 

Daestrum

Expert
Licensed User
Longtime User
% is a special character in a url , it means the following number is the character code of the character I really want.

ie,
Your text_data post_data should read 10%25%20off
which is 10(% = %25)(space = %20)off

There are ways to URLencode data so that it is a valid URL. In javascript look at encodeURI().
 
Last edited:
Upvote 0

pmt

Member
Licensed User
More Information:

Raw data from sender (client app) : 10% Off ABC

For B4J Server:

===============================================
My Json_Decode Function:
B4X:
Sub Json_Decode(obj As String) As Map
   
    Dim su As StringUtils
    Dim parser As JSONParser
    Dim data As String = obj
   
    parser.Initialize(su.DecodeUrl(data,"utf8"))
   
    'parser.Initialize(data)
    Dim root As Map = parser.NextObject
   
    If Not (root.IsInitialized) Then
        root.Initialize
    End If
   
    Return root
   
End Sub

====================================================

This from Main Function:

B4X:
Dim data() As Byte = Bit.InputStreamToBytes(in)
Dim text_data As String = BytesToString(data, 0, data.Length, "UTF8") '
Log("Post Raw Data:"&text_data)


I Get:
Post Raw Data:{"action":"action1","data":"{\"master\":{\"name\":\"10%25%20Off%20%20ABC\"},\"items\":{\"code\":\"0003\"}}","page":0}

B4X:
Dim root As Map = MyUtility.Json_Decode(text_data)
Log("post:"&root)

I Get:
post:(MyMap) {data={"master":{"name":"10% Off ABC"},"items":{"code":"0003"}}, action=action1, page=0}

B4X:
Dim mdata As Map =MyUtility.Json_Decode(root.Get("data"))

I Get Error :
(IllegalArgumentException) java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: " O"




For sender side (client application) I use > name: encodeURI( '10% Off ABC' )

I don't know how to fix this error.


Regards,
PMT
 
Last edited:
Upvote 0

pmt

Member
Licensed User
I think the DecodeUrl(data,"utf8") function need the correct format data , the "10% Off ABC " is wrong format.

So I create another Json_decode function and change
parser.Initialize(su.DecodeUrl(data,"utf8")) to => parser.Initialize(data)

B4X:
Sub Json_Decode2(obj As String) As Map
  
    Dim su As StringUtils
    Dim parser As JSONParser
    Dim data As String = obj
  
    parser.Initialize(data)
  
    'parser.Initialize(data)
    Dim root As Map = parser.NextObject
  
    If Not (root.IsInitialized) Then
        root.Initialize
    End If
  
    Return root
  
End Sub

and use:
B4X:
Dim mdata As Map =MyUtility.Json_Decode2(root.Get("data"))

Work!

But I don't sure this is the good idea ?

Regards,
PMT
 
Upvote 0
Top