第一次写简书文章,不足之处下方评论处请提出,谢谢
百度翻译开放平台
点击链接,右上角登录/注册,认证为个人开发者,选择通用翻译,然后
立即使用
点击立即使用,然后
点击文档与支持,左侧通用翻译API技术文档,查看接口信息
请求字段
网络请求返回的字段
例子:
将apple从英文翻译成中文:
请求参数:
q=apple
from=en
to=zh
appid=2015063000000001(在管理控制台左面的开发者信息里查看)
salt=1435660288(任意值)
平台分配的密钥: 12345678 (在管理控制台左面的开发者信息里查看)
生成sign:
拼接字符串1拼接:
appid+q+sal+密钥=2015063000000001apple143566028812345678
计算签名sign(对字符串1做md5加密,注意计算md5之前,串1必须为UTF-8编码)sign=md5(2015063000000001apple143566028812345678)sign
=f89f9594663708c1605f3d736d01d2d4
完整请求为:http://api.fanyi.baidu.com/api/trans/vip/translate?q=apple&from=en&to=zh&appid=2015063000000001&salt=1435660288&sign=f89f9594663708c1605f3d736d01d2d4
也可以使用POST方法传送需要的参数。
MD5加密方法
private static String md5(String string) {
if (TextUtils.isEmpty(string))
return "";
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
byte[] bytes = md5.digest(string.getBytes());
String result = "";
for (byte b : bytes) {
String temp = Integer.toHexString(b & 0xff);
if (temp.length() == 1) {
temp = "0" + temp;
}
result += temp;
}
return result;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
语言列表和错误码列表我就不贴了,有兴趣自己看看
下面开始在Android Studio上写代码
1.添加依赖
as自带的okhttp版本有点老,所以自己导入下好的okhttp包,还有okio包,一起使用,我们这里使用的是Gson解析json,所以添加Gson依赖
拷贝到libs文件夹下,右键选择as a library
注意依赖是否缺少
2.AndroidManifest.xml添加网络权限
3.代码
javabean类:Translate.class使用GsonFormat生成
public class Translate {
/**
* from : en
* to : zh
* trans_result : [{"src":"apple","dst":"苹果"}]
*/
private String from;
private String to;
/**
* src : apple
* dst : 苹果
*/
private List trans_result;
public void setFrom(String from) {
this.from = from;
}
public void setTo(String to){
this.to = to;
}
public void setTrans_result(List trans_result) {
this.trans_result = trans_result;
}
public String getFrom() {
return from;
} public String getTo() {
return to;
}
public List getTrans_result() {
return trans_result;
}
public static class TransResultEntity {
private String src;
private String dst;
public void setSrc(String src) {
this.src = src;
}
public void setDst(String dst) {
this.dst = dst;
}
public String getSrc() {
return src;
}
public String getDst() {
return dst;
}
}
}
布局activity_main.xml
textview,editview,button
MainActivity.class
public class MainActivity extends AppCompatActivity {
private EditText mEditText; //输入框
private TextView mTextView;//用来显示查询结果
String json; //返回的json串
private static final String url = "http://api.fanyi.baidu.com/api/trans/vip/translate"; //网络请求的url
private static final String APP_ID_STRING = "20180407000143709"; //自己的appid
private static final String SALT_STRING = "1435660288"; //随机数可以随便设置
private static final String SECRET_STRING = "LXCjF_t1j9qOgwgzu95B"; //自己的密钥
private String SIGN_STRING;//签名,md5加密
private String string;//拼接需要md5加密的字符串
//创建okhttp对象,设置超时
private OkHttpClient mOkHttpClient = new OkHttpClient
.Builder()
.connectTimeout(5000, TimeUnit.SECONDS)
.build();
//handle来通知主线程处理UI更新
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
mTextView.setText(msg.getData().getString("translate"));
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEditText = (EditText) findViewById(R.id.editText_translate);
mTextView = (TextView) findViewById(R.id.textView_show); findViewById(R.id.button_translate).setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
Request_Get();
}
});
}
private void Request_Get() {
//拼接需要md5加密的字符串
string = APP_ID_STRING + mEditText.getText().toString()+SALT_STRING+SECRET_STRING;
SIGN_STRING = md5(string);//把字符串转为md5
//用formbody来添加字段
FormBody body = new FormBody
.Builder()
.add("from", "en")
.add("to", "zh")
.add("q", mEditText.getText().toString())
.add("appid", APP_ID_STRING)
.add("salt", SALT_STRING)
.add("sign", SIGN_STRING)
.build();
//创建Request对象
Request request = new Request
.Builder()
.post(body) //post请求
.url(url)
.build();
//创建call对象,开始异步网络请求
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_LONG).show();
}
@Override public void onResponse(Call call, Response response) throws IOException {
//返回的结果为json
json = response.body().string();
//开线程
new Thread() {
@Override
public void run() {
super.run();
//创建javabean对象实例
Translate mTranslate = new Translate();
//创建gson对象实例
Gson gson = new Gson();
//开始解析json
mTranslate = gson.fromJson(json, Translate.class);
//使用message和bundle来传递信息
Message msg = Message.obtain();
Bundle b = new Bundle();
b.putString("translate", mTranslate.getTrans_result().get(0).getDst() + "");
msg.setData(b);
//交给handler处理
mHandler.sendMessage(msg);
}
}.start();
}
});
}
private static String md5(String string) {
if (TextUtils.isEmpty(string))
return "";
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
byte[] bytes = md5.digest(string.getBytes());
String result = "";
for (byte b : bytes) {
String temp = Integer.toHexString(b & 0xff);
if (temp.length() == 1) {
temp = "0" + temp;
}
result += temp;
} return result;
} catch (Exception e) {
e.printStackTrace();
} return "";
}
}