Baidu translation interface JAVA version

1. In Baidu translation platform, apply for an account. After applying, you will get APP_ID and SECURITY_KEY

2. The java code is as follows

import net.sf.json.JSONObject;

import com.baidu.translate.demo.TransApi;

public class Main {

    // See http://api.fanyi.baidu.com/api/trans/product/desktop?req=developer for the APP_ID applied for on the platform
    private static final String APP_ID = "app_id applied for";
    private static final String SECURITY_KEY = "application security_key";

    public static void main(String[] args) {
        TransApi api = new TransApi(APP_ID, SECURITY_KEY);
         String query = "Peach's Yaoyao is shining brightly. The son is returning home, and it is suitable for his family.";
          String str=api.getTransResult(query, "auto", "en");
         System.out.println(str);
         JSONObject json=JSONObject.fromObject(str.toString());
         String str1=json.get("trans_result").toString();
         // remove []
          str1=str1.replace("[", "");
          str1=str1.replace("]", "");
          JSONObject json1=JSONObject.fromObject(str1);
         System.out.println("Source language: "+json1.get("src")+"--language type"+json.get("from"));
         System.out.println("target language:"+json1.get("dst")+"--language type"+json.get("to"));
          
    }

}
3. The effect is as follows:


4. Download the complete code as follows:

Click to open the link

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325389409&siteId=291194637