首页 在线翻译 在线翻译示例代码 翻译[Java]

翻译示例代码[Java]

作者:xiezhongpian 阅读数:3427 上传时间:2017-05-09

翻译

package api.jisuapi.translate;

import java.net.URLEncoder;

import api.util.HttpUtil;
import net.sf.json.JSONObject;

public class Translate {

	public static final String APPKEY = "your_appkey_here";// 你的appkey
	public static final String URL = "https://api.jisuapi.com/translate/translate";
	public static final String type = "google";
	public static final String from = "zh-CN";
	public static final String to = "en";
	public static final String text = "好";// utf-8

	public static void Get() throws Exception {
		String result = null;
		String url = URL + "?appkey=" + APPKEY + "&type=" + type + "&from=" + from + "&to=" + to + "&text="
				+ URLEncoder.encode(text, "utf-8");

		try {
			result = HttpUtil.sendGet(url, "utf-8");
			JSONObject json = JSONObject.fromObject(result);
			if (json.getInt("status") != 0) {
				System.out.println(json.getString("msg"));
			} else {
				JSONObject resultarr = json.optJSONObject("result");
				String text = resultarr.getString("result");
				System.out.println(text);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}