Android Question [Solved] Additional character while using ByteToString

rraswisak

Active Member
Licensed User
Hi all,

My app has this sub:

B4X:
Sub ConvertData(strWeb As String) As List
   strWeb = "eJyVkjsLwjAURhXBH5JZJa2Kjy2kUYpJbslNu4gEB3FxEyfxvxvR4ar10Tnn5LuvdrfTaq3PzOyOx+1+x+asyHzQwiv0oVIOc7Csx6rt4XR7TAfJgLNL700AGRYODEGTDxgKU+jcLinKf7BBgvUOtFauQQKxgsltE9MIK5bNwh5K0yQsC+WqHIGGTf+3XvI+mR7oFsc1VCEQ48QyRcAZT1LOh3W0A6migD6eCjEWQqP6wldCl5Sv2zx6kKtYSmn9Hz/f6ey5DD7pZ0r2U55M6xVH6dE4tri5Aofk45E="
   Dim su As StringUtils
   Dim data() As Byte = su.DecodeBase64(strWeb)
   
   Dim zip As CompressedStreams
   Dim extract() As Byte
   extract = zip.DecompressBytes(data,"zlib")
   Dim json As JSONParser
   
   Dim xx As String = BytesToString(extract,0,extract.Length,"utf-8")
   Log(xx) '<---- additional character at front
   json.Initialize(xx)

   Dim list As List
   list = json.NextArray

   Return list
End Sub

As we can see there is additional unknown character as log below

sshoot.jpg


Same code i have try in B4J work just fine.

Any one has similar situation as mine ?

Thank you
 

Douglas Farias

Expert
Licensed User
Longtime User
this chars comes from your json response.
you need use trim to fix this before convert to bytes.

change this
B4X:
Dim data() As Byte = su.DecodeBase64(strWeb)
to
B4X:
Dim data() As Byte = su.DecodeBase64(strWeb.Trim)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
change this
it does work but it is the wrong solution.

This is the HEX-Code from the decompressed JSON.
The first 5 bytes are Binary. 0106030000

Where does the bytes come from is the question.

B4X:
01060300005B7B224D657373616765223A225044545F4C41544553545F56455253494F4E222C2256616C7565223A22322E312E30227D2C7B224D657373616765223A225044545F4C4F435F46524F4D222C2256616C7565223A2231227D2C7B224D657373616765223A225044545F4C4F435F53414D504C494E47222C2256616C7565223A223130227D2C7B224D657373616765223A225044545F4C4F435F53414D504C494E475F434F4E54524F4C4C4552222C2256616C7565223A2231227D2C7B224D657373616765223A225044545F4C4F435F53414D504C494E475F434F4E54524F4C4C45525F4D494E222C2256616C7565223A2231227D2C7B224D657373616765223A225044545F4C4F435F53414D504C494E475F4D414E41474552222C2256616C7565223A2231227D2C7B224D657373616765223A225044545F4C4F435F53414D504C494E475F4D414E414745525F4D494E222C2256616C7565223A2231227D2C7B224D657373616765223A225044545F4C4F435F53414D504C494E475F53555045525649534F52222C2256616C7565223A2238227D2C7B224D657373616765223A225044545F4C4F435F53414D504C494E475F53555045525649534F525F4D494E222C2256616C7565223A2238227D2C7B224D657373616765223A225044545F4C4F435F544F222C2256616C7565223A223235227D2C7B224D657373616765223A225044545F504153535F434F4445222C2256616C7565223A2239303132303033227D2C7B224D657373616765223A225044545F50524F434553535F5354415445222C2256616C7565223A2246414C5345227D2C7B224D657373616765223A225044545F50524F434553535F56414C5545222C2256616C7565223A2230227D2C7B224D657373616765223A225044545F53544F434B5F434F554E54222C2256616C7565223A2246414C5345227D2C7B224D657373616765223A225044545F53544F434B5F44415445222C2256616C7565223A2230372D4445432D32303138227D2C7B224D657373616765223A225044545F53544F5245222C2256616C7565223A22343533227D5D
 
Upvote 0

rraswisak

Active Member
Licensed User
Hi @DonManfred, the data came as response of my server web service created with asp.net, i use SharpZipLib B4XSerializator to compress and return the result as response.

Here is the code (C#) at server side:
B4X:
DataRow dr = null;
OracleCommand cmd = new OracleCommand("Select * From AppMsg Where Message Like 'PDT_%' Order By Message", conn);
OracleDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
     dr = dt.NewRow();
     dr["Message"] = rdr["Message"];
     dr["Value"] = rdr["Value"];
     dt.Rows.Add(dr);
}

string json = JsonConvert.SerializeObject(dt);
                           
B4XSerializator ser = new B4XSerializator();
byte[] zipData = ser.ConvertObjectToBytes(json);
string zipString = Convert.ToBase64String(zipData, 0, zipData.Length);

HttpContext.Current.Response.Write(zipString);

Where does the bytes come from is the question
I dont have any idea where is the byte came from...
 
Upvote 0

rraswisak

Active Member
Licensed User
@Erel, The above code is the most shortest respond from server, it's less than 50 records return so without compressing is not a problem, mean while there is also process to transfer master data from server to device which is 800 thousand records and getting bigger and bigger, so the app can work offline.

Compressing result data from server is a must i think, so i choose SharpZipLib to handle with. I have test the code in B4J it's work fine, please take a look:

b4j.jpg


Anything else i missed?, thank you
 
Upvote 0

rraswisak

Active Member
Licensed User
I wouldn't use B4XSerializator here
Of course there is a hidden message here...

Change the plan, I remove the B4XSerializator class at the server side and change the code as below:

B4X:
DataRow dr = null;
OracleCommand cmd = new OracleCommand("Select * From AppMsg Where Message Like 'PDT_%' Order By Message", conn);
OracleDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
     dr = dt.NewRow();
     dr["Message"] = rdr["Message"];
     dr["Value"] = rdr["Value"];
     dt.Rows.Add(dr);
}

string json = JsonConvert.SerializeObject(dt);
                           
/**
B4XSerializator ser = new B4XSerializator();
byte[] zipData = ser.ConvertObjectToBytes(json);
string zipString = Convert.ToBase64String(zipData, 0, zipData.Length);
**/

byte[] bytData = System.Text.Encoding.UTF8.GetBytes(json);
MemoryStream ms = new MemoryStream();
Stream s = new DeflaterOutputStream(ms);
s.Write(bytData, 0, bytData.Length);
s.Close();
byte[] compressedData = (byte[])ms.ToArray();
string zipString = Convert.ToBase64String(compressedData, 0, compressedData.Length);

HttpContext.Current.Response.Write(zipString);

now they all are work as expected :)
thank you all
 
Upvote 0
Top