Android 中文字符轉UTF-8編碼

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;


public class Test {
    public static void main(String[] args) {
        String str = "測試字符轉換 hello word"; //默認環境,已是UTF-8編碼
        try {
            String strGBK = URLEncoder.encode(str, "GBK");
            System.out.println(strGBK);
            String strUTF8 = URLDecoder.decode(str, "UTF-8");
            System.out.println(strUTF8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}

今天Android工程中需要組成URL地址,其中keyword搜索關鍵字爲中文,需要轉爲UTF-8格式,網上搜索到以上代碼,問題解決。


參考:http://s.yanghao.org/program/viewdetail.php?i=61509



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章