String codification to Twitter
I'm developing a program that sends tweets. I have this piece of code:
StringBuilder sb = new StringBuilder("Recomendo ");
sb.append(lblName.getText());
sb.append(" no canal "+lblCanal.getText());
sb.append(" no dia "+date[2]+"/"+date[1]+"/"+date[0]);
sb.append(" às "+time[0]+"h"+time[1]);
byte[] defaultStrBytes = sb.toString().getBytes("ISO-8859-1");
String encodedString = new Str开发者_如何学运维ing(defaultStrBytes, "UTF-8");
But When I send it to tweet I get the "?" symbol or other strage characters because of the accents like "à" . I've also tried with only
String encodedString = new String(sb.toString().getBytes(), "UTF-8"); //also tried with ISO-8859-1
but the problem remains...
You are trying to read Latin-1 as UTF-8. That's why you are getting question marks.
Try to send your string as is,
String encodedString = sb.toString();
The charset should be taking care when you send the message to Tweet. If URL encoding is required, you would do something like
String msg = URLEncoder.encode(encodedString, "UTF-8");
精彩评论