先去 http://fanyi.youdao.com/openapi?path=data-mode 申请开发者key

  有道api会自动将申请的单词翻译并返回为xml或者json格式,我们所需要做的就是将返回的格式显示到屏幕上即可

MainActivity.java

package cn.lixyz.youdaodictionary;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; public class MainActivity extends Activity { private EditText word;
private Button submit;
private TextView translation; private String str = "http://fanyi.youdao.com/openapi.do?keyfrom=AndroidHttpTest&key=507293865&type=data&doctype=json&version=1.1&q=";
String requestStr; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initView(); submit.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
String tmpWord = word.getText().toString().trim();
requestStr = str + tmpWord;
new Thread(new Runnable() {
@Override
public void run() {
URL url;
try {
url = new URL(requestStr);
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String tmpLine = "";
String line = null;
while ((line = br.readLine()) != null) {
tmpLine = tmpLine + line;
}
Message msg = new Message();
msg.obj = tmpLine;
handler.sendMessage(msg); } catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
});
} private void initView() {
word = (EditText) findViewById(R.id.word);
submit = (Button) findViewById(R.id.submit);
translation = (TextView) findViewById(R.id.translation);
} Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
// translation.setText(msg.obj.toString()); try {
JSONObject object = new JSONObject(msg.obj.toString());
JSONObject jsonobject = object.getJSONObject("basic");
String usPhonetic = jsonobject.getString("us-phonetic");
String phonetic = jsonobject.getString("phonetic");
String ukPhonetic = jsonobject.getString("uk-phonetic");
JSONArray array = jsonobject.getJSONArray("explains");
String showText = "";
for (int i = 0; i < array.length(); i++) {
String obj = array.getString(i);
showText = showText + obj + "\n";
}
showText = "美音:\t" + usPhonetic + "\n" + "英音:\t" + ukPhonetic + "\n" + "\n\n" + showText; translation.setText(showText);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} };
}; }

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.lixyz.youdaodictionary.MainActivity" > <EditText
android:id="@+id/word"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" /> <Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="翻译" /> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="翻译为:" /> <TextView
android:id="@+id/translation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" /> </LinearLayout>

  因为要请求网络连接,所以需要加上INTENET权限

<uses-permission android:name="android.permission.INTERNET"/>

Android笔记(五十三) 利用有道OPENAPI做简单的翻译demo的更多相关文章

  1. Android基础笔记(十三)- 内容提供者原理和简单使用

    为什么要有内容提供者 内容提供者的工作原理 使用内容解析者对内容提供者进行增删改查操作 利用内容提供者和内容解析者备份手机短信 利用内容提供者插入短信 为什么要有内容提供者 内容提供者技术的目的是: ...

  2. Android笔记三十三.BroadcastReceiver使用

        广播是一种广泛运用在应用程序之间传输信息的机制,而BroadcastReceiver是对发送出来的广播进行过滤接收并响应的一类组件. BroadcastReceiver本质上是一种全局监听器. ...

  3. 论文阅读笔记五十三:Libra R-CNN: Towards Balanced Learning for Object Detection(CVPR2019)

    论文原址:https://arxiv.org/pdf/1904.02701.pdf github:https://github.com/OceanPang/Libra_R-CNN 摘要 相比模型的结构 ...

  4. Android笔记(七十三) Android权限问题整理 非常全面

    Android权限系统非常庞大,我们在Android系统中做任何操作都需要首先获取Android系统权限,本文记录了所有的Android权限问题,整理一下分享给大家. 访问登记属性 android.p ...

  5. Android笔记(六十三) android中的动画——逐帧动画( frame-by-frame animation)

    就好像演电影一样,播放实现准备好的图片,来实现动画效果. 逐帧动画需要用到AnimationDrawable类,该类主要用于创建一个逐帧动画,然后我们把这个动画设置为view的背景即可. androi ...

  6. Android笔记(十三) Android中的基本组件——文本

    Android中常用的文本组件有 普通文本框(TextView)和编辑框(EditText)两种 EditText是TextView的子类,作用就是在界面上显示文本,区别是EditText允许用户编辑 ...

  7. Android学习笔记(十三)

    Android中的广播机制 Android提供了一套完整的API,允许应用程序自由地发送和接受广播. 发送广播的方法借助于Intent,接受广播的方法需要广播接收器(BroadcastsReceive ...

  8. Android学习笔记(十三)SharedPreference必须掌握的基础

    我们在开发中,应用程序会保存少量数据,例如一些字符串.一些标记或者一些配置文件,这时候如果去使用SQLite保存这些数据的话,难免会显得大材小用,用起来也不方便,对于这种信息,保存在SharedPre ...

  9. 孤荷凌寒自学python第五十三天使用python写入和修改Firebase数据库中记录

     孤荷凌寒自学python第五十三天使用python写入和修改Firebase数据库中记录 (完整学习过程屏幕记录视频地址在文末) 今天继续研究Firebase数据库,利用google免费提供的这个数 ...

随机推荐

  1. linux环境,无dig命令-bash: dig: command not found?

    背景描述: 今天使用dig命令,报错命令不存在,-bash: dig: command not found 解决: 通过yum方式安装 yum -y install bind-utils 备注:之前尝 ...

  2. tomcat乱码解决

    一.修改Tomcat的conf的server.xml文件加上 URIEncoding="UTF-8" 二.在tomcat的bin 目录下的catalina.bat 配置文件中,添加 ...

  3. 关于C语言中的结构体内嵌函数(转)

    https://blog.csdn.net/qq_39490500/article/details/80457831 看门见山 1.内嵌函数定义举例:经过真实测试 在函数中声明定义结构体 #inclu ...

  4. [LeetCode] 68. Text Justification 文本对齐

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  5. [LeetCode] 156. Binary Tree Upside Down 二叉树的上下颠倒

    Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...

  6. [LeetCode] 206. Reverse Linked List 反向链表

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

  7. [LeetCode] 305. Number of Islands II 岛屿的数量 II

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  8. hexo 博客如何更换电脑

    如何在更换电脑后继续使用Hexo部署博客 重要目录 _config.yml package.json scaffolds/ source/ themes/ 在新电脑上配置hexo环境:安装node.j ...

  9. 在ensp上的mstp基础配置

    为什么需要mstp? 因为stp中存在阻塞端口,阻塞后不承载流量,造成了带宽浪费 实验模拟 实验拓扑 相关参数 首先我们在交换机上创建vlan 10,20 设置端口 默认是运行mstp服务看一下

  10. seaborn做横向条形图

    数据格式如下 这里选择fold值最大的前20个作图进行展示 代码如下 import seaborn as sns import pandas as pd import numpy as np impo ...