I've a tcp connection and i try to send and receive messages in byte, but I don't know how it work.
Here is my code to send:
public void write(String message) {
try {
byte[] b = message.getBytes("UTF-8");
writer.write(b.toString());
writer.newLine();
writer.flush();
} catch (IOException e) {
Log.e(MainActivity.TAG, "exception", e);
e.printStackTrace();
} catch (Exception e) {
Log.e(MainActivity.TAG, "exception", e);
e.printStackTrace();
}
}
and this for receive
String message = client.reader.readLine();
JSONObject json = new JSONObject(message);
I try again :)
How I can convert the received byte array to string with full unicode for the mysql database? I use charset Utf8m4 for my database.
This is my code
byte[] message = System.Text.Encoding.UTF8.GetBytes(client.reader.ReadLine()); // client.reader.ReadLine() is already a byte[]
string encoded = System.Text.Encoding.UTF8.GetString(message);
JToken token = JObject.Parse(encoded);
My code isn't work. I get:
{"Id":"Test","Content":"hey???"}
byte[] b = message.getBytes("UTF-8");
writer.write(b.toString());
If this is the code you're asking abut, it is senseless. It should of course be
writer.write(message);
At present you're sending something of the form [B@NNNNNNNNNNN
, which is the hashcode of a byte array.
I've found a solution, thank's to @EJP.
In java (client side)
writer.write(B64.encode(message)); // message is a string
In C# (server side)
string encoded = System.Text.Encoding.UTF8.GetString(message);
JToken token = JObject.Parse(encoded);
To save the bytes in database i use blob as type.
Encoding.UTF8.GetBytes(bluuub)
To get the bytes from database
byte[] b = (byte[])reader.GetValue(0);
string bluuub = Encoding.UTF8.GetString(b);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.