drstein
drstein

Reputation: 1113

Utf-8 encoding for Android

i have a problem with utf-8 encoding.

I retrieve from a MySQL Database data formatted in UTF-8 (with a lot of chinese symbols) with Java and i put them on 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 that i use to populate some Activities on my Android App but, not all the symbols are correctly displayed: the most of them are correct, but some are not recognized and the're shown as a black diamond with a white question mark inside.

i've also tried with this: but i got worst 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 idea? thanks!

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 on the DB the query:

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

i get the result

utf8

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

Upvotes: 5

Views: 25034

Answers (4)

phnghue
phnghue

Reputation: 1696

In case you want to read text file in SD Card:

public String readText(Uri fileUri){ String zS,sOut=""; boolean kE10=false; try {
    BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(getContentResolver().openFileDescriptor(fileUri, "r").getFileDescriptor()), StandardCharsets.UTF_8));
    while ((zS = in.readLine()) != null){if (kE10){ sOut+=(char)10+zS; } else {sOut+=zS;} kE10=true;}
} catch (Exception e) {} return sOut;}

Upvotes: 0

n0rmzzz
n0rmzzz

Reputation: 3848

You need to figure out in which phase this happens: 1- Where you load the text from database, and 2- where you write them out to the file.

One thing to note is that your database must be created with UTF-8 encoding and your connection must also support it. An example of a JDBC connection url that supports UTF-8 can be:

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

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

Also try converting from an encoding you suspect the data is in (like ISO-8859-1):

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

Upvotes: 11

William Kinaan
William Kinaan

Reputation: 28819

for connection to Database, I used this for all UTF-8 and it works good

This is a class you can use it, just edit the information about your username, passwor, and database name

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 this

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

on the android (or any reciever of the data) try this

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

edit

for you code you can add this

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

Upvotes: 1

Shreyash Mahajan
Shreyash Mahajan

Reputation: 23606

@houman001 is right. You should have to take care during that two procedure.

In Addition:

I have same problem with Arabic text before some days. But now i have solved it. I have solve it by making it to write in to database with UTF-8 encoding. and also have use some Arabic font given by my font developer. Which result me in to the text in Arabic as i want.

So, please take care during parsing and transferring data from one to another and make sure that it is in UTF-8 Encoding format.

Hope this thing helps you.

Upvotes: 1

Related Questions