第一步,申请API key,申请地址:http://fanyi.youdao.com/openapi?path=data-mode
数据接口:
版本:1.1,请求方式:get,编码方式:utf-8
主要功能:中英互译,同时获得有道翻译结果和有道词典结果(可能没有)
参数说明:
type - 返回结果的类型,固定为data
doctype - 返回结果的数据格式,xml或json或jsonp
version - 版本,当前最新版本为1.1
q - 要翻译的文本,不能超过200个字符,需要使用utf-8编码
errorCode:
0 - 正常
20 - 要翻译的文本过长
30 - 无法进行有效的翻译
40 - 不支持的语言类型
50 - 无效的key
json数据格式举例
请求链接:http://fanyi.youdao.com/openapi.do?keyfrom=<keyfrom>&key=<key>&type=data&doctype=json&version=1.1&q=翻译
返回的json数据:
{
"errorCode":0
"query":"翻译",
"translation":["translation"], // 有道翻译
"basic":{ // 有道词典-基本词典
"phonetic":"fān yì",
"explains":[
"translate",
"interpret"
]
},
"web":[ // 有道词典-网络释义
{
"key":"翻译",
"value":["translator","translation","translate","Interpreter"]
},
{...}
]
}
下面实现android客户端功能。
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/a">
<EditText
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="输入你要查询的内容......" />
<Button
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="查询" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none" >
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"/>
</ScrollView>
</LinearLayout>
实现效果如下:
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText edit = null;
private Button search = null;
private TextView text = null;
private String YouDaoBaseUrl = "http://fanyi.youdao.com/openapi.do";
private String YouDaoKeyFrom = "你的daoKeyFrom";
private String YouDaoKey = "你的api key";
private String YouDaoType = "data";
private String YouDaoDoctype = "json";
private String YouDaoVersion = "1.1";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init() {
edit = (EditText) findViewById(R.id.edit);
search = (Button) findViewById(R.id.search);
search.setOnClickListener(new searchListener());
text = (TextView) findViewById(R.id.text);
}
private class searchListener implements OnClickListener {
@Override
public void onClick(View v) {
String YouDaoSearchContent = edit.getText().toString().trim();
String YouDaoUrl = YouDaoBaseUrl + "?keyfrom=" + YouDaoKeyFrom
+ "&key=" + YouDaoKey + "&type=" + YouDaoType + "&doctype="
+ YouDaoDoctype + "&type=" + YouDaoType + "&version="
+ YouDaoVersion + "&q=" + YouDaoSearchContent;
try {
AnalyzingOfJson(YouDaoUrl);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void AnalyzingOfJson(String url) throws Exception {
// 第一步,创建HttpGet对象
HttpGet httpGet = new HttpGet(url);
// 第二步,使用execute方法发送HTTP GET请求,并返回HttpResponse对象
HttpResponse httpResponse = new DefaultHttpClient().execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// 第三步,使用getEntity方法活得返回结果
String result = EntityUtils.toString(httpResponse.getEntity());
System.out.println("result:" + result);
JSONArray jsonArray = new JSONArray("[" + result + "]");
String message = null;
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
if (jsonObject != null) {
String errorCode = jsonObject.getString("errorCode");
if (errorCode.equals("20")) {
Toast.makeText(getApplicationContext(), "要翻译的文本过长",
Toast.LENGTH_SHORT);
} else if (errorCode.equals("30 ")) {
Toast.makeText(getApplicationContext(), "无法进行有效的翻译",
Toast.LENGTH_SHORT);
} else if (errorCode.equals("40")) {
Toast.makeText(getApplicationContext(), "不支持的语言类型",
Toast.LENGTH_SHORT);
} else if (errorCode.equals("50")) {
Toast.makeText(getApplicationContext(), "无效的key",
Toast.LENGTH_SHORT);
} else {
// 要翻译的内容
String query = jsonObject.getString("query");
message = query;
// 翻译内容
String translation = jsonObject
.getString("translation");
message += "\t" + translation;
// 有道词典-基本词典
if (jsonObject.has("basic")) {
JSONObject basic = jsonObject
.getJSONObject("basic");
if (basic.has("phonetic")) {
String phonetic = basic.getString("phonetic");
message += "\n\t" + phonetic;
}
if (basic.has("explains")) {
String explains = basic.getString("explains");
message += "\n\t" + explains;
}
}
// 有道词典-网络释义
if (jsonObject.has("web")) {
String web = jsonObject.getString("web");
JSONArray webString = new JSONArray("[" + web + "]");
message += "\n网络释义:";
JSONArray webArray = webString.getJSONArray(0);
int count = 0;
while (!webArray.isNull(count)) {
if (webArray.getJSONObject(count).has("key")) {
String key = webArray.getJSONObject(count)
.getString("key");
message += "\n\t<" + (count + 1) + ">"
+ key;
}
if (webArray.getJSONObject(count).has("value")) {
String value = webArray
.getJSONObject(count).getString(
"value");
message += "\n\t " + value;
}
count++;
}
}
}
}
}
text.setText(message);
} else {
Toast.makeText(getApplicationContext(), "提取异常", Toast.LENGTH_SHORT);
}
}
}
运行效果如下:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>