本文主要是介绍安卓小项目之刀刀狗翻译,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
学了一阵安卓,想搞个实用的app玩玩。
项目名称:刀刀狗翻译
api源:有道api
界面如下,略简陋
mainactivity
package com.example.zheng.fangyi; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.method.ScrollingMovementMethod; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast;import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element;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; public class MainActivity extends AppCompatActivity {private EditText input;private TextView result;String rdline=new String();String read=null;@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);input = (EditText) findViewById(R.id.input);result = (TextView) findViewById(R.id.result);result.setMovementMethod(ScrollingMovementMethod.getInstance());Button check = (Button) findViewById(R.id.check);try {check.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {new AsyncTask<String,Void,Void>(){@Override protected Void doInBackground(String... params) {try {URL url=new URL(params[0]);try {URLConnection conn=url.openConnection();InputStream is=conn.getInputStream();InputStreamReader isr=new InputStreamReader(is,"utf-8");BufferedReader br=new BufferedReader(isr);String line;while((line=br.readLine())!=null){System.out.println(line);rdline+=line;}br.close();isr.close();is.close();} catch (IOException e) {e.printStackTrace();}} catch (MalformedURLException e) {e.printStackTrace();}return null;}}.execute("http://fanyi.youdao.com/openapi.do?keyfrom=zsdlove&key=5668291&type=data&doctype=xml&version=1.1&q="+input.getText());xmlresolve(rdline);//解析xml rdline="";read="";}});}catch (Exception e) {e.printStackTrace();}}public void xmlresolve(String rdline)//xml解析 {try {Document document = DocumentHelper.parseText(rdline);org.dom4j.Element root=document.getRootElement();//获取根节点 //错误捕获========== Element errorCode=root.element("errorCode");if(errorCode.getText().equals("20")){Toast.makeText(getApplicationContext(),"要翻译的文本过长",Toast.LENGTH_SHORT);}else if(errorCode.getText().equals("30")){Toast.makeText(getApplicationContext(),"无法进行有效的翻译",Toast.LENGTH_SHORT);}else if(errorCode.getText().equals("40")){Toast.makeText(getApplicationContext(),"不支持的语言类型",Toast.LENGTH_SHORT);}else if(errorCode.getText().equals("50")){Toast.makeText(getApplicationContext(),"无效的key",Toast.LENGTH_SHORT);}else if(errorCode.getText().equals("60")){Toast.makeText(getApplicationContext(),"请输入正确的英文",Toast.LENGTH_SHORT);}//错误捕获finish================= else{org.dom4j.Element tnote=root.element("translation");//获取translation节点 Element pnote=tnote.element("paragraph");//获取paragraph节点 //=================== Element basic =root.element("basic");//basic节点 Element us=basic.element("us-phonetic");//美式音标 Element uk=basic.element("uk-phonetic");//uk音标 Element explains=basic.element("explains");//基本释义 Element ex=explains.element("ex");read="翻译结果:"+"\r\n"+"常规翻译:"+pnote.getText()+"\r\n"+"美式音标:"+us.getText()+"\r\n"+"英式音标:"+uk.getText()+"\r\n"+"名词解释:"+ex.getText();}} catch (DocumentException e) {e.printStackTrace();}result.setText(read);} }========================================================================================================================
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" 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="com.example.zheng.fangyi.MainActivity"><Button android:text="查询" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/check" android:layout_below="@+id/input" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="12dp" /><TextView android:text="翻译结果:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="67dp" android:id="@+id/result" android:layout_alignTop="@+id/check" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /><TextView android:text="zsd出品" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="32dp" android:id="@+id/textView" /><EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPersonName" android:text="请输入" android:ems="10" android:id="@+id/input" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /></RelativeLayout>
输入hello
查看效果
这里提一下,对于xml返回文件的解析需要用到dom4j.jar包,可以到官网上去下载。引入项目中的libs目录中就可以了,libs目录找不到的原因是目录查看的方式不对
这篇关于安卓小项目之刀刀狗翻译的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!