百度在线翻译API接入文档:http://fanyi-api.baidu.com/api/trans/product/apidoc
本博客Demo地址:http://download.csdn.net/detail/g_ying_jie/9890682
第一步、注册后开发者权限获取APPID和密钥
该接口申请参数如下
第二步、获取用户输入的需要翻译的内容并转换成接口可以识别的形式
private void dealWithField() {
String str = edit_query.getText().toString();
if (str != null && str.length() > 0) {
q = str;
}
q = Utils.toUtf8(q);
String l = appid + q + salt + key;
sign = Utils.encrypt(l);
q = Utils.toURLDecoded(q);
en = Utils.toURLDecoded(en);
zh = Utils.toURLDecoded(zh);
}
public class Utils {
public static String toURLDecoded(String paramString) {
if (paramString == null || paramString.equals("")) {
return "";
}
try {
String str = new String(paramString.getBytes(), "UTF-8");
str = URLDecoder.decode(str, "UTF-8");
return str;
} catch (Exception localException) {
}
return "";
}
public static String toUtf8(String str) {
String result = null;
try {
result = new String(str.getBytes("UTF-8"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
/**
* 加密
* @param plaintext 明文
* @return ciphertext 密文
*/
public final static String encrypt(String plaintext) {
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
try {
byte[] btInput = plaintext.getBytes();
// 获得MD5摘要算法的 MessageDigest 对象
MessageDigest mdInst = MessageDigest.getInstance("MD5");
// 使用指定的字节更新摘要
mdInst.update(btInput);
// 获得密文
byte[] md = mdInst.digest();
// 把密文转换成十六进制的字符串形式
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
return null;
}
}
}
OkHttpUtils
.get()
.url("http://api.fanyi.baidu.com/api/trans/vip/translate")
.addParams("q", q)
.addParams("from", en)
.addParams("to", zh)
.addParams("appid", appid)
.addParams("salt", salt)
.addParams("sign", sign)
.build()
.execute(new TransCallBack() {
@Override
public void onError(Call call, Exception e, int id) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
@Override
public void onResponse(TransResult response, int id) {
tv_result.setText(response.getTrans_result().get(0).getDst());
}
});
compile 'com.zhy:okhttputils:2.6.2'
compile 'com.alibaba:fastjson:1.2.34'
public abstract class TransCallBack extends Callback {
@Override
public TransResult parseNetworkResponse(Response response, int id) throws Exception {
String s = response.body().string();
TransResult result = JSON.parseObject(s, TransResult.class);
return result;
}
}
{"from":"en","to":"zh","trans_result":[{"src":"apple","dst":"\u82f9\u679c"}]}
public class TransResult {
/**
* from : en
* to : zh
* trans_result : [{"src":"apple","dst":"苹果"}]
*/
private String from;
private String to;
private List trans_result;
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public List getTrans_result() {
return trans_result;
}
public void setTrans_result(List trans_result) {
this.trans_result = trans_result;
}
public static class TransResultBean {
/**
* src : apple
* dst : 苹果
*/
private String src;
private String dst;
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
public String getDst() {
return dst;
}
public void setDst(String dst) {
this.dst = dst;
}
}
}
下载安装,重启as,然后新建一个Javabean类
使用Alt+Insert或者Alt+s快捷键,将返回的json内容完整的粘贴进去
点击OK生成一个完好的Javabean类
注意fastjson对格式要求很高,出现了不符合标准的属性名可用以下方法转化
@JSONField(name = "total_join_user")
private int userCount;