• 利用有道API进行翻译


    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.net.URLEncoder;
    import java.util.List;
    import java.util.Map;


    public class YoudaoTranslate {

     
     private String url = "http://fanyi.youdao.com/openapi.do";

     
     private String keyfrom = "";
     private String key = "";

     
     private String doctype = "xml";

     
     private String sendGet(String str) {

      // 编码成UTF-8
      try {
       str = URLEncoder.encode(str, "utf-8");
      } catch (UnsupportedEncodingException e) {
       e.printStackTrace();
      }

      StringBuffer buf = new StringBuffer();
      buf.append("keyfrom=");
      buf.append(keyfrom);
      buf.append("&key=");
      buf.append(key);
      buf.append("&type=data&doctype=");
      buf.append(doctype);
      buf.append("&version=1.1&q=");
      buf.append(str);

      String param = buf.toString();

      String result = "";
      BufferedReader in = null;
      try {
       String urlName = url + "?" + param;
       URL realUrl = new URL(urlName);

       // 打开和URL之间的连接
       URLConnection conn = realUrl.openConnection();

       // 设置通用的请求属性
       // conn.setRequestProperty("accept", "*
     public String getYouDaoValue(String str) {
      String result = null;

      // 发送GET请求翻译
      result = sendGet(str);

      // 处理XML中的值
      int re1 = result.indexOf("<errorCode>");
      int re2 = result.indexOf("</errorCode>");
      String in = result.substring(re1 + 11, re2);
      System.out.println("===========翻译是否成功============" + in);

      if (in.equals("0")) {
       System.out.println("翻译正常");

       re1 = result.indexOf("<paragraph><![CDATA[");
       re2 = result.indexOf("]]></paragraph>");
       in = result.substring(re1 + 20, re2);
       System.out.println("==========有道翻译================" + in);

       re1 = result.indexOf("<ex><![CDATA[");
       re2 = result.indexOf("]]></ex>");
       in = result.substring(re1 + 13, re2);
       System.out.println("==========有道词典-网络释义================" + in);

      } else if (in.equals("20")) {
       System.out.println("要翻译的文本过长");
       return "要翻译的文本过长";
      } else if (in.equals("30")) {
       System.out.println("无法进行有效的翻译");
       return "无法进行有效的翻译";
      } else if (in.equals("40")) {
       System.out.println("不支持的语言类型");
       return "不支持的语言类型";
      } else if (in.equals("50")) {
       System.out.println("无效的key");
       return "无效的key";
      }

      return result;
     }

     public static void main(String[] args) {

      String str = "The weather is good today";

      YoudaoTranslate test = new YoudaoTranslate();
      String temp = test.getYouDaoValue(str);
      System.out.println(temp);
     }
    }

  • 相关阅读:
    ubuntu安装打印机驱动
    clang-format的介绍和使用
    [C++面向对象]-C++成员函数和非成员函数
    [Qt2D绘图]-06QPainter的复合模式&&双缓冲绘图&&绘图中的其他问题
    [Qt2D绘图]-05绘图设备-QPixmap&&QBitmap&&QImage&&QPicture
    [Qt2D绘图]-04绘制文字&&绘制路径
    [Qt2D绘图]-03坐标系统之坐标变换
    [Qt2D绘图]-02坐标系统&&抗锯齿渲染
    [Qt2D绘图]-01Qt2D绘图基本绘制和填充
    [Qt插件]-01Qt插件&&提升部件(自定义控件)
  • 原文地址:https://www.cnblogs.com/bbsno1/p/3278075.html
Copyright © 2020-2023  润新知