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