有道翻译API+Jackson+HttpClient
在学习Redis的阻塞队列的时候,想到了传入JSON,然后出队列,在这个基础上,又想到了分布式,而这个JSON串来自于远程服务器,所以需要用HttpClient来执行这个请求响应网络服务器API的职能,后来看到了有道翻译API就顺手写了一个测试类。
第一步,新建一个Java Project
第二步,到apache官网下载httpclient 4.5项目,将jar包放到lib目录下
第三步,到百度下载jackson项目及Junit4项目的jar包,放到lib目录下
第四步,到有道翻译官网申请api接口
第五步,建立一些需要的class类
YouDao.java
package com.dby.json;
import java.util.Arrays;
/** * Created by suzunshou on 2016/5/12. */
public class YouDao {
private String[] translation;
private String query;
private Basic basic;
private int errorCode;
private Web[] web;
public String[] getTranslation() {
return translation;
}
public void setTranslation(String[] translation) {
this.translation = translation;
}
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
}
public Basic getBasic() {
return basic;
}
public void setBasic(Basic basic) {
this.basic = basic;
}
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
public Web[] getWeb() {
return web;
}
public void setWeb(Web[] web) {
this.web = web;
}
@Override
public String toString() {
return "YouDao{" +
"translation=" + Arrays.toString(translation) +
", query='" + query + '\'' +
", basic=" + basic +
", errorCode=" + errorCode +
", web=" + Arrays.toString(web) +
'}';
}
}
Basic.java
package com.dby.json;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Arrays;
/** * Created by suzunshou on 2016/5/12. */
public class Basic {
@JsonProperty("us-phonetic")
private String us_phonetic;
@JsonProperty("uk-phonetic")
private String uk_phonetic;
private String phonetic;
private String[] explains;
public String getUs_phonetic() {
return us_phonetic;
}
public void setUs_phonetic(String us_phonetic) {
this.us_phonetic = us_phonetic;
}
public String getUk_phonetic() {
return uk_phonetic;
}
public void setUk_phonetic(String uk_phonetic) {
this.uk_phonetic = uk_phonetic;
}
public String getPhonetic() {
return phonetic;
}
public void setPhonetic(String phonetic) {
this.phonetic = phonetic;
}
public String[] getExplains() {
return explains;
}
public void setExplains(String[] explains) {
this.explains = explains;
}
@Override
public String toString() {
return "Basic{" +
"us_phonetic='" + us_phonetic + '\'' +
", uk_phonetic='" + uk_phonetic + '\'' +
", phonetic='" + phonetic + '\'' +
", explains=" + Arrays.toString(explains) +
'}';
}
}
Web.java
package com.dby.json;
import java.util.Arrays;
/** * Created by suzunshou on 2016/5/12. */
public class Web {
private String key;
private String[] value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String[] getValue() {
return value;
}
public void setValue(String[] value) {
this.value = value;
}
@Override
public String toString() {
return "Web{" +
"key='" + key + '\'' +
", value=" + Arrays.toString(value) +
'}';
}
}
第六步,新建一个测试类TestCase.java
package com.dby.json;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** * Created by suzunshou on 2016/5/12. */
public class TestCase {
@Test
public void testNet() throws Exception {
ObjectMapper mapper = new ObjectMapper();
CloseableHttpClient httpClient = HttpClients.createDefault();
URI url = new URIBuilder("http://fanyi.youdao.com/openapi.do")
.build();
HttpPost HttpPost = new HttpPost(url.toString());
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("keyfrom", "atmoon"));
params.add(new BasicNameValuePair("key", "1407453772"));
params.add(new BasicNameValuePair("type", "data"));
params.add(new BasicNameValuePair("doctype", "json"));
params.add(new BasicNameValuePair("version", "1.1"));
params.add(new BasicNameValuePair("q", "hello"));
HttpPost.setEntity(new UrlEncodedFormEntity(params));
CloseableHttpResponse response = httpClient.execute(HttpPost);
String json = EntityUtils.toString(response.getEntity());
System.out.println(json);
YouDao youDao = mapper.readValue(json, YouDao.class);
System.out.println(youDao.toString());
}
}
返回结果
{"translation":["你好"],"basic":{"us-phonetic":"hɛˈlo, hə-","phonetic":"hə'ləʊ; he-","uk-phonetic":"hə'ləʊ; he-","explains":["n. 表示问候, 惊奇或唤起注意时的用语","int. 喂;哈罗","n. (Hello)人名;(法)埃洛"]},"query":"hello","errorCode":0,"web":[{"value":["你好","您好","hello"],"key":"Hello"},{"value":["凯蒂猫","昵称","匿称"],"key":"Hello Kitty"},{"value":["哈乐哈乐","乐扣乐扣"],"key":"Hello Bebe"}]}
YouDao{translation=[你好], query='hello', basic=Basic{us_phonetic='hɛˈlo, hə-', uk_phonetic='hə'ləʊ; he-', phonetic='hə'ləʊ; he-', explains=[n. 表示问候, 惊奇或唤起注意时的用语, int. 喂;哈罗, n. (Hello)人名;(法)埃洛]}, errorCode=0, web=[Web{key='Hello', value=[你好, 您好, hello]}, Web{key='Hello Kitty', value=[凯蒂猫, 昵称, 匿称]}, Web{key='Hello Bebe', value=[哈乐哈乐, 乐扣乐扣]}]}