I pass a JsonObject consisting of objects of type String from the android application to the server via a POST request and save the variables in MySQL using JDBC. If the String variables contain only English letters, everything works correctly. But when trying to save Cyrillic or the characters "", "ẞ", etc. the server gives the error:

Incorrect string value: '\xC2\x92 \xC3\x91\xC2...' for column 'contacts' at row 1

What I have done:

  • Set text file encoding to UTF-8 in Eclipse-> Window-> Preferences-> Workspace
  • Set UTF-8 as Default encoding for all content types in Eclipse-> Window-> Preferences-> General-> Content Types
  • Replaced DB_URL with "jdbc:mysql://localhost/YOURWAY?useUnicode=yes&characterEncoding=UTF-8" Before this action, MySQL saved Cyrillic, but partially, in the form: "Add ?? o ?? e ?? o"

UPDATE:

  • Replaced the encoding of all tables and databases with commands: ALTER DATABASE название_базы CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci; database_name ALTER DATABASE название_базы CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci; and ALTER TABLE название_таблицы CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
  • Replaced DB_URL with "jdbc: mysql: // localhost / YOURWAY? UseUnicode = yes & characterEncoding = UTF-8 & character_set_server = utf8mb4"

The error no longer crashes, but now when you call SELECT * FROM название_базы database_name, MySQL produces something like “à° òà° ù ÃÂÂÂÃÂöøÃÂÃÂÂÂ! At the same time, if you get this data on Android via a GET request, Android again displays "Add ?? o ?? e ut ?? o".

What am I missing?

  • jdbc: mysql: // localhost: 3306 / users? useUnicode = true & amp; characterEncoding = UTF-8 & amp; characterSetResults = UTF-8 - EmErIx_007
  • and encoding in mysql when installing, set to utf-8 general-c - EmErIx_007
  • @ EMERIKS_007 Please explain in more detail. Do I need to return the encoding of the tables and database in utf8? How to install encoding when installing? - Serafim Dmitriev
  • after installation, it will ask you to start the mysql program (customizer). It contains all the parameters: a password, a variable for the environment, etc. There you have it, there is an encoding selection point. - EmErIx_007

1 answer 1

So, I found a problem. This was not MySQL at all.

The problem was in the okhttp library as part of retrofit2, which I use to communicate with the server. Okhttp does not allow direct transfer of the Cyrillic alphabet in GET and POST requests. To circumvent this limitation, I added encoding before transferring data to the server using URLEncoder.encode(строка, "UTF-8"); and decoding after receiving data from the server using URLDecoder.decode(строка, "UTF-8"); . In addition, it was necessary to increase the length of all VARCHAR columns in MySQL three times (since one Cyrillic character in String corresponds to three characters in UTF-8). I hope my experience will help someone else.