Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
72 views
in Technique[技术] by (71.8m points)

how/unable to convert garbled/strange text to utf-8 android (java)?

I have garbled text è??¥ which is returned by web service (php) fetched from MySql

Now I am trying to decode it to utf-8 in android, but it's not working

I have tried:

String s = "è??¥";// text returned by web service taking it as static for testing

1. not working:

String str = new String(s.getBytes(), "utf-8");

2. not working:

String normalized = Normalizer.normalize(str, Normalizer.Form.NFD);
// also tried NFC, NFKC, NFKD
// also tested by isNormalized its returning true 

3. not working:

String str =URLDecoder.decode(s, "utf-8");

all above are giving same output: è??¥

So, please can anyone help me understand what I am doing wrong? Or please provide me any alternative?

Any help will be much appreciated. Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As Stephen C explained very well i followed all that steps, but little additional changes are required :

1. As Stephen C explained my server was sending data in Latin-1 encoding so i have to use ISO8859_1 charset

2. i was trying String str = new String(s.getBytes(), "utf-8");

this will not work for Latin-1 encoded data !

so for this i have to set the charset (for my case ISO8859_1) of data to getBytes(" ISO8859_1")

so this is working fine now

String str = new String(s.getBytes("ISO-8859-1"), "utf-8");

Note second parameter is for charset of new string so it must be utf-8 to display the original text


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...