第一步,申请API key,申请地址:http://fanyi.youdao.com/openapi?path=data-mode

数据接口:

http://fanyi.youdao.com/openapi.do?keyfrom=<keyfrom>&key=<key>&type=data&doctype=<doctype>&version=1.1&q=要翻译的文本

版本: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>

实现效果如下:

 
MainActivity.java
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);
}
}
}

运行效果如下:

 
 
最后,勿忘添加Manifest.xml权限
    <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"/>

Android有道词典开发相关推荐

  1. android简单的有道词典开发

    简单的android有道词典开发 第一次写教程,不好勿怪哈!其实想写这篇教程已经很久了,但却一直没有付诸行动,这个项目是偶然间在论坛里发现的,我算是弄出来整理了一下吧!所以在此要感谢那些前辈们,没有他 ...

  2. Android进阶2之有道词典开发

    本博文只是实现有道词典的功能,并着重界面. 首先,你需要获取有道开发平台的API key.点击打开链接 申请一个吧. 利用数据接口获取数据: http://fanyi.youdao.com/opena ...

  3. android学习-有道词典开发实例

    最近学习android程序开发,在网看上到一个关于android手机开发有道词典的例子.但是,并不能正常运行,现在个人改进版本源代码和思路献上之供学习之用. 第一步,申请API key,申请地址:ht ...

  4. android有道词典简单开发

    Android剪切板(ClipBoardManager)复制的内容,可以粘贴到任何地方,对于一些词典,翻译工具等app具有较高的使用价值.有道词典在3.6版本后就使用到该功能,本文来剖析具体的实现过程 ...

  5. Android有道词典查询功能

    有道词典 任务要求:完成查词等功能 因为需要申请API key,这里直接给出地址供使用:http://fanyi.youdao.com/openapi?path=data-mode 1.activit ...

  6. 关于android有道词典的修改

    本文是关于第一篇博文里的项目布局的修改 将main.xml里面的绝对布局改为线性布局即可,感觉这样看起来舒服些! <?xml version="1.0" encoding=& ...

  7. 简易词典Android界面代码,Android 有道词典的简单实现方法介绍

    第一步:思路解析 从界面看一共用了三个控件EditText,Button,WebView.其实是四个,是当我们查询内容为空的时候用来提示的Toast控件.我们在EditText输入查询内容,这里包括中 ...

  8. android 网页词典,android 有道词典查询单词(webview版)

    [实例简介] [实例截图] [核心代码] package com.example.youdaodictionary; import android.app.Activity; import andro ...

  9. 有道词典之父:曾梦想执笔去教书,却转岗当上CEO

    周枫分别于2000年和2002年在清华大学获得计算机学士和硕士学位,2007年于加州大学伯克利分校获得博士学位,主要研究兴趣是面向大规模服务的软件基础设施.操作系统和编程语言. 现任网易高级副总裁,网 ...

最新文章

  1. 来看看CDN网络安全防护的方案
  2. 如何画出几种常见二分类损失函数(附代码)
  3. 动态时间规整_动态规划-数组系列(10%)
  4. 2013年08月13日
  5. linux-2.6.38 input子系统(用输入子系统实现按键操作)
  6. python3 collections模块 tree_第30天: Python collections 模块
  7. echarts做企业关系图谱_建立良好客户关系 做有温度的企业
  8. 鸿蒙技术论坛,鸿蒙应用开发入门(六):页面间跳转
  9. vue 切换页面没有改变滚动条_Web前端高级Vue学习笔记(三)
  10. 站立会议03--个人总结
  11. python tkinter
  12. 机器学习识别电子数字-制作字体文件
  13. 微博html5版开视频怎么退出,微博怎么取消视频号?微博视频号怎么关闭
  14. Red Giant 安装及爆炸效果详解
  15. Python实现回文
  16. 盛世乐居回应近期股价波动
  17. 利用gpu加速神经网络算法,外接gpu 训练神经网络
  18. ICLR 2022论文双盲通过却被爆抄袭:数据算法全部照搬,第二页几乎空白
  19. NIST伪随机测试出现igamc:UNDERFLOW的原因以及测试文件的格式
  20. 高质量前端快照方案:来自页面的「自拍」

热门文章

  1. 【山东seo】-淄博孔祥永seo技术分享博客
  2. 2014广州入户新规则--广州积分入户8月1日起接受申报 详细指引
  3. DARKHOLE_1攻略
  4. arm64的ioremap_ARMv8 内存管理架构.学习笔记
  5. 《C++程序设计》第十章总结
  6. 在电脑上通过手机发短信
  7. Python之水仙花数问题解决
  8. 【DS1302驱动】
  9. FontAwesome 图标 class=fa fa-home
  10. C# 更换微信小程序码中间的logo图层