首页 > 代码库 > Android使用有道翻译API实如今线翻译功能

Android使用有道翻译API实如今线翻译功能

在Android应用中,加入在线翻译的功能,这里调用的是有道翻译的API。

使用有道翻译API。首先要申请一个key,申请地址为:有道翻译API申请地址。

技术分享

申请之后就会得到一个keyfrom和一个key。

获取翻译结果的数据接口为:http://fanyi.youdao.com/openapi.do?

keyfrom=<keyfrom>&key=<key>&type=data&doctype=<doctype>&version=1.1&q=要翻译的文本。我们仅仅须要把内容拼接成这样。使用GET的方式。就能得到翻译的结果。

我翻译了“程序猿”,接口返回的数据为:

{
    "translation": [
        "The programmer"
    ],
    "basic": {
        "phonetic": "chéng xù yuán",
        "explains": [
            "programmer"
        ]
    },
    "query": "程序猿",
    "errorCode": 0,
    "web": [
        {
            "value": [
                "Programmer",
                "CODER",
                "SCJP"
            ],
            "key": "程序猿"
        },
        {
            "value": [
                "Systems Programmer",
                "Java Card",
                "system programmer"
            ],
            "key": "系统程序猿"
        },
        {
            "value": [
                "programmer",
                "computer programmer",
                "Job-InterviewComputer Programmer"
            ],
            "key": "电脑程序猿"
        }
    ]
}
我们仅仅须要从中解析出我们所须要的内容就可以。

详细实现例如以下:

public class MainActivity extends Activity {

	private EditText edit = null;
	private TextView search = null;
	private TextView text = null;
	private String YouDaoBaseUrl = "http://fanyi.youdao.com/openapi.do";
	private String YouDaoKeyFrom = "YouDaoKeyFrom";
	private String YouDaoKey = "YouDaoKey";
	private String YouDaoType = "data";
	private String YouDaoDoctype = "json";
	private String YouDaoVersion = "1.1";
	private TranslateHandler handler;

	private static final int SUCCEE_RESULT = 10;
	private static final int ERROR_TEXT_TOO_LONG = 20;
	private static final int ERROR_CANNOT_TRANSLATE = 30;
	private static final int ERROR_UNSUPPORT_LANGUAGE = 40;
	private static final int ERROR_WRONG_KEY = 50;
	private static final int ERROR_WRONG_RESULT = 60;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		edit = (EditText) findViewById(R.id.edit);
		search = (TextView) findViewById(R.id.search);
		search.setOnClickListener(new searchListener());
		text = (TextView) findViewById(R.id.text);
		handler = new TranslateHandler(this, text);
	}

	private class searchListener implements OnClickListener {
		@Override
		public void onClick(View v) {

			String content = edit.getText().toString().trim();
			if (content == null || "".equals(content)) {
				Toast.makeText(getApplicationContext(), "请输入要翻译的内容", Toast.LENGTH_SHORT).show();
				return;
			}
			final String YouDaoUrl = YouDaoBaseUrl + "?keyfrom=" + YouDaoKeyFrom + "&key=" + YouDaoKey + "&type="
					+ YouDaoType + "&doctype=" + YouDaoDoctype + "&type=" + YouDaoType + "&version=" + YouDaoVersion
					+ "&q=" + content;
			new Thread() {
				public void run() {
					try {
						AnalyzingOfJson(YouDaoUrl);
					} catch (Exception e) {
						e.printStackTrace();
					}
				};
			}.start();
		}
	}

	private void AnalyzingOfJson(String url) throws Exception {
		// 第一步,创建HttpGet对象
		HttpGet httpGet = new HttpGet(url);
		// 第二步,使用execute方法发送HTTP GET请求。并返回HttpResponse对象
		HttpResponse httpResponse = new DefaultHttpClient().execute(httpGet);
		if (httpResponse.getStatusLine().getStatusCode() == 200) {
			// 第三步。使用getEntity方法活得返回结果
			String result = EntityUtils.toString(httpResponse.getEntity());
			System.out.println("result:" + result);
			JSONArray jsonArray = new JSONArray("[" + result + "]");
			String message = null;
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				if (jsonObject != null) {
					String errorCode = jsonObject.getString("errorCode");
					if (errorCode.equals("20")) {
						handler.sendEmptyMessage(ERROR_TEXT_TOO_LONG);
					} else if (errorCode.equals("30 ")) {
						handler.sendEmptyMessage(ERROR_CANNOT_TRANSLATE);
					} else if (errorCode.equals("40")) {
						handler.sendEmptyMessage(ERROR_UNSUPPORT_LANGUAGE);
					} else if (errorCode.equals("50")) {
						handler.sendEmptyMessage(ERROR_WRONG_KEY);
					} else {
						Message msg = new Message();
						msg.what = SUCCEE_RESULT;
						// 要翻译的内容
						String query = jsonObject.getString("query");
						message = "翻译结果:";
						// 翻译内容
						Gson gson = new Gson();
						Type lt = new TypeToken<String[]>() {
						}.getType();
						String[] translations = gson.fromJson(jsonObject.getString("translation"), lt);
						for (String translation : translations) {
							message += "\t" + translation;
						}
						// 有道词典-基本词典
						if (jsonObject.has("basic")) {
							JSONObject basic = jsonObject.getJSONObject("basic");
							if (basic.has("phonetic")) {
								String phonetic = basic.getString("phonetic");
								// message += "\n\t" + phonetic;
							}
							if (basic.has("explains")) {
								String explains = basic.getString("explains");
								// message += "\n\t" + explains;
							}
						}
						// 有道词典-网络释义
						if (jsonObject.has("web")) {
							String web = jsonObject.getString("web");
							JSONArray webString = new JSONArray("[" + web + "]");
							message += "\n网络释义:";
							JSONArray webArray = webString.getJSONArray(0);
							int count = 0;
							while (!webArray.isNull(count)) {

								if (webArray.getJSONObject(count).has("key")) {
									String key = webArray.getJSONObject(count).getString("key");
									message += "\n(" + (count + 1) + ")" + key + "\n";
								}
								if (webArray.getJSONObject(count).has("value")) {
									String[] values = gson.fromJson(webArray.getJSONObject(count).getString("value"),
											lt);
									for (int j = 0; j < values.length; j++) {
										String value = http://www.mamicode.com/values[j];"。";
										}
									}
								}
								count++;
							}
						}
						msg.obj = message;
						handler.sendMessage(msg);
					}
				}
			}
			text.setText(message);
		} else {
			handler.sendEmptyMessage(ERROR_WRONG_RESULT);
		}
	}

	private class TranslateHandler extends Handler {
		private Context mContext;
		private TextView mTextView;

		public TranslateHandler(Context context, TextView textView) {
			this.mContext = context;
			this.mTextView = textView;
		}

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SUCCEE_RESULT:
				mTextView.setText((String) msg.obj);
				closeInput();
				break;
			case ERROR_TEXT_TOO_LONG:
				Toast.makeText(mContext, "要翻译的文本过长", Toast.LENGTH_SHORT).show();
				break;
			case ERROR_CANNOT_TRANSLATE:
				Toast.makeText(mContext, "无法进行有效的翻译", Toast.LENGTH_SHORT).show();
				break;
			case ERROR_UNSUPPORT_LANGUAGE:
				Toast.makeText(mContext, "不支持的语言类型", Toast.LENGTH_SHORT).show();
				break;
			case ERROR_WRONG_KEY:
				Toast.makeText(mContext, "无效的key", Toast.LENGTH_SHORT).show();
				break;
			case ERROR_WRONG_RESULT:
				Toast.makeText(mContext, "提取异常", Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	}

	public void closeInput() {
		InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		if ((inputMethodManager != null) && (this.getCurrentFocus() != null)) {
			inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),
					InputMethodManager.HIDE_NOT_ALWAYS);
		}
	}
}
看一下效果:

技术分享

技术分享

补充:翻译的文本应该要经过编码才干够,防止特殊字符。

 URLEncoder.encode(content);

Demo下载地址:http://download.csdn.net/detail/u014375869/8844145

Android使用有道翻译API实如今线翻译功能