Utf-8 encoding for Android

I have a problem with utf-8 encoding.

I am pulling from MySQL database data formatted in UTF-8 (with a lot of Chinese characters) from Java and I put them in some txt files: (this is an example .. I have a lot of data)

String name = null;
ResultSet res = stat.executeQuery("Some_SQL");
nomeImg = res.getString("value");

PrintWriter = new new PrintWriter(new FileOutputStream("file_name.txt"));
out.println(name);

      

with these txt files I will create some TextView which I use to fill in some actions in my android app but not all characters are displayed correctly: most of them are correct but some are not recognized and they are shown as black diamond with white question mark inside.

I tried with this too: but I got worse results

byte[] name = null;
ResultSet res = stat.executeQuery("Some_SQL");
nomeImg = res.getString("value").getBytes("UTF-8"):

BufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("file_name.txt"), "UTF-8"));
out.write(new String(name, "UTF-8")+"\n");

      

Does anyone have any ideas? thank!

edit i connect to my db with:

Connection con = DriverManager.getConnection("jdbc:mysql://url:port?useUnicode=true&characterEncoding=utf-8", user, pass);

      

and when I execute a query in the DB:

SELECT default_character_set_name 
FROM information_schema.SCHEMATA S 
WHERE schema_name = "schema_name";

      

We get the result

utf8

      

so I think the DB is encoded in UTF-8 and my connection to it can handle UTF-8

+3


source to share


3 answers


You need to figure out in what phase this is happening: 1- Where do you load text from the database and 2- you write them to a file.

It should be noted that your database must be created with UTF-8 encoding and your connection must support it as well. An example JDBC connection URL that supports UTF-8 might be:

jdbc:mysql://hostname:port/schema?useUnicode=yes&characterEncoding=utf-8

      



The symptom you see after specifying the encoding in getBytes ("UTF-8") is a clear indication that what you are getting from the database is not in UTF-8.

Also try converting from the encoding you suspect of the data (e.g. ISO-8859-1):

new String(request.getParameter("value").getBytes("ISO-8859-1"), "UTF-8");

      

+9


source


@ houman001 is right. You should take care of this during two procedures.

In addition:

I have the same problem with Arabic text up to several days. But now I have solved it. I solved it by making it to write to the database with UTF-8 encoding. and also use some Arabic font provided by my font developer. Which brings me to the Arabic text as I want.



So, be careful when parsing and transferring data from one to the other and make sure it is in UTF-8 encoding format.

Hope this thing helps you.

+1


source


to connect to the database i used this for all UTF-8 and works well

This is a class you can use, just edit your username, password and database name information

String unicode = "?useUnicode=yes&characterEncoding=UTF-8";
    String driverName = "com.mysql.jdbc.Driver";
    String hostName = "localhost";
    String port = "3306";
    String databaseName = "yourDatabaseName";

String userName = "yourusername";
    String password = "yourpassword";
    String databaseType = "jdbc:mysql";
    public Connection con;

    public Database() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = (Connection) DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/yourdatabasename" + unicode,
                    "yourusername", "yourpassword");
        } catch (ClassNotFoundException e) {
            System.err
                    .println("exception Class (" + driverName + ") not found");
            e.printStackTrace();
        } catch (SQLException e) {
            System.err.println("exception Connection URL");
            e.printStackTrace();
        }
    }

      

when you want to send data from server to android try

ServletOutputStream out = response.getOutputStream();
out.println(URLEncoder.encode(yourString,"UTF-8"));

      

on android (or any data sink) try this

String dd = URLDecoder.decode(yourReturnedData, "UTF-8");

      

change

for code you can add this

name = URLDecoder.decode(name, "UTF-8");

      

+1


source







All Articles