1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| package com.ilubov.util.youdao;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Maps; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils;
import java.util.Map;
public class YoudaoUtil {
public static final String TRANSLATE_URL = "http://fanyi.youdao.com/translate?&doctype=json&type=AUTO&i=%s";
public static void main(String[] args) { translate("一"); }
public static void translate(String word) { Map<String, Object> result = get(String.format(TRANSLATE_URL, word)); System.out.println(JSON.toJSONString(result, true)); }
public static Map<String, Object> get(String url) { HttpGet req = new HttpGet(url); try (CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse resp = client.execute(req)) { String text = EntityUtils.toString(resp.getEntity(), "UTF-8"); JSONObject json = JSON.parseObject(text); return json.getInnerMap(); } catch (Exception e) { e.printStackTrace(); } return Maps.newHashMap(); } }
|