P pmt Member Licensed User May 31, 2018 #1 Hi everyone, I use B4J 5.9 and I have a problem about decodeurl. I get data from the client application. Code: 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. Code: Dim su As StringUtils Log("text_data:"&su.DecodeUrl(text_data,"utf8")) I get error : Code: (IllegalArgumentException) java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: " O" How to fix this error ? Regards, PMT.
Hi everyone, I use B4J 5.9 and I have a problem about decodeurl. I get data from the client application. Code: 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. Code: Dim su As StringUtils Log("text_data:"&su.DecodeUrl(text_data,"utf8")) I get error : Code: (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 May 31, 2018 #2 % 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: May 31, 2018 Upvote 0
% 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().
P pmt Member Licensed User May 31, 2018 #3 More Information: Raw data from sender (client app) : 10% Off ABC For B4J Server: =============================================== My Json_Decode Function: Code: 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: Code: 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} Code: Dim root As Map = MyUtility.Json_Decode(text_data) Log("post:"&root) I Get: postMyMap) {data={"master":{"name":"10% Off ABC"},"items":{"code":"0003"}}, action=action1, page=0} Code: 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: May 31, 2018 Upvote 0
More Information: Raw data from sender (client app) : 10% Off ABC For B4J Server: =============================================== My Json_Decode Function: Code: 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: Code: 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} Code: Dim root As Map = MyUtility.Json_Decode(text_data) Log("post:"&root) I Get: postMyMap) {data={"master":{"name":"10% Off ABC"},"items":{"code":"0003"}}, action=action1, page=0} Code: 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
P pmt Member Licensed User May 31, 2018 #4 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) Code: 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: Code: 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
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) Code: 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: Code: Dim mdata As Map =MyUtility.Json_Decode2(root.Get("data")) Work! But I don't sure this is the good idea ? Regards, PMT