public static String translateByBaidu(String param) {
String path = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=alGwKqkBfMbM0T3lbvR3onuZ&q="+param+"&from=auto&to=auto";
String content = "";
String translate_value = null;
try {
URL url = new URL(path);
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setDoInput(true);
http.setDoOutput(true);
http.setConnectTimeout(20000);
http.setReadTimeout(20000);
http.connect();
int respCode = http.getResponseCode();
if (200 == respCode) {
System.out.println("百度翻译响应成功");
InputStream in = http.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String line = null;
while((line = reader.readLine()) != null) {
content+=line;
}
//Json 解析
JSONObject json = JSON.parseObject(content);
JSONArray arr = (JSONArray) json.get("trans_result");
JSONObject json_value = JSON.parseObject(arr.getString(0));
translate_value = json_value.get("dst").toString();
System.out.println("baidu API fanyi :"+translate_value);
translate_value = translate_value.replaceAll("'", "''");
}else{
try {
Thread.sleep(4*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return translate_value;
}
String path = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=alGwKqkBfMbM0T3lbvR3onuZ&q="+param+"&from=auto&to=auto";
String content = "";
String translate_value = null;
try {
URL url = new URL(path);
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setDoInput(true);
http.setDoOutput(true);
http.setConnectTimeout(20000);
http.setReadTimeout(20000);
http.connect();
int respCode = http.getResponseCode();
if (200 == respCode) {
System.out.println("百度翻译响应成功");
InputStream in = http.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String line = null;
while((line = reader.readLine()) != null) {
content+=line;
}
//Json 解析
JSONObject json = JSON.parseObject(content);
JSONArray arr = (JSONArray) json.get("trans_result");
JSONObject json_value = JSON.parseObject(arr.getString(0));
translate_value = json_value.get("dst").toString();
System.out.println("baidu API fanyi :"+translate_value);
translate_value = translate_value.replaceAll("'", "''");
}else{
try {
Thread.sleep(4*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return translate_value;
}