这里是主函数部分 将英文转换为中文
String query="hello man";//输入想要翻译的英文
TransApi api = new TransApi(APP_ID, SECURITY_KEY);
String s = api.getTransResult(query, "en", "zh");//api翻译后得到结果
int a =s.lastIndexOf("\"");
int b=s.lastIndexOf("\"", a-1);
String w= s.substring(b+1, a);//提取结果字符串
String unicode = w;
String ret = null;
String[] rets = unicode.split("\\\\u"); //分解成一个个16进制Unicode编码
char[] chars = new char[rets.length];
int i = 0;
for(String ss:rets){
if(ss.equals(""))continue;
chars[i] = (char)Integer.parseInt(ss,16);//将16进制转换为10进制,再强转为char
i++;
}
ret = new String(chars);
try {
ret = new String(ret.getBytes("utf-8"),"utf-8");//得到结果utf-8编码格式的结果
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ret = ret.trim();//去除末尾空格
System.out.println(ret);//输出