• 安卓--有道词典


    第一步:想要实现有道词典的开发,你必须申请一个API Key,申请链接http://fanyi.youdao.com/openapi。

    什么是有道翻译API?

    有道翻译API,为广大开发者提供开放接口。您的应用或网站可通过有道翻译API,构建丰富多样的功能或应用,为用户带来即时,准确,方便的查词或翻译体验,从而降低语言理解与应用门槛。

    依靠有道翻译网络数据挖掘和统计机器翻译技术实力做基础,加上您的智慧,将创造出更优良的体验和创新的功能。

    2:利用数据接口获得数据:

      『1』版本:1.1  

      『2』  网页请求方式:get 

      『3』编码方式:utf-8

      『4』主要功能:中英互译,同时获得有道翻译结果和有道词典结果(可能没有)

      『5』参数说明:

       type - 返回结果的类型,固定为data

      doctype - 返回结果的数据格式,xml或json或jsonp

      version - 版本,当前最新版本为1.1

      q - 要翻译的文本,不能超过200个字符,需要使用utf-8编码

    errorcode:

     0 - 正常

     20 - 要翻译的文本过长

     30 - 无法进行有效的翻译

     40 - 不支持的语言类型

     50 - 无效的key

    json数据格式举例

     1     http://fanyi.youdao.com/openapi.do?keyfrom=<keyfrom>&key=<key>&type=data&doctype=json&version=1.1&q=翻译  
     2     {  
     3         "errorCode":0  
     4         "query":"翻译",  
     5         "translation":["translation"], // 有道翻译  
     6         "basic":{ // 有道词典-基本词典  
     7             "phonetic":"fān yì",  
     8             "explains":[  
     9                 "translate",  
    10                 "interpret"  
    11             ]  
    12         },  
    13         "web":[ // 有道词典-网络释义  
    14             {  
    15                 "key":"翻译",  
    16                 "value":["translator","translation","translate","Interpreter"]  
    17             },  
    18             {...}  
    19         ]  
    20     }  

    xml数据格式:

      1 本博文只是实现有道词典的功能,并着重界面。
      2 
      3 首先,你需要获取有道开发平台的API key。点击打开链接 申请一个吧。
      4 
      5 
      6 
      7 利用数据接口获取数据:
      8 
      9 版本:1.1,请求方式:get,编码方式:utf-8
     10 
     11 主要功能:中英互译,同时获得有道翻译结果和有道词典结果(可能没有)
     12 
     13 参数说明:
     14 
     15  type - 返回结果的类型,固定为data
     16 
     17  doctype - 返回结果的数据格式,xml或json或jsonp
     18 
     19  version - 版本,当前最新版本为1.1
     20 
     21  q - 要翻译的文本,不能超过200个字符,需要使用utf-8编码
     22 
     23 errorCode:
     24 
     25  0 - 正常
     26 
     27  20 - 要翻译的文本过长
     28 
     29  30 - 无法进行有效的翻译
     30 
     31  40 - 不支持的语言类型
     32 
     33  50 - 无效的key
     34 
     35 返回的样本:
     36 
     37 json数据格式举例
     38 
     39     http://fanyi.youdao.com/openapi.do?keyfrom=<keyfrom>&key=<key>&type=data&doctype=json&version=1.1&q=翻译  
     40     {  
     41         "errorCode":0  
     42         "query":"翻译",  
     43         "translation":["translation"], // 有道翻译  
     44         "basic":{ // 有道词典-基本词典  
     45             "phonetic":"fān yì",  
     46             "explains":[  
     47                 "translate",  
     48                 "interpret"  
     49             ]  
     50         },  
     51         "web":[ // 有道词典-网络释义  
     52             {  
     53                 "key":"翻译",  
     54                 "value":["translator","translation","translate","Interpreter"]  
     55             },  
     56             {...}  
     57         ]  
     58     }  
     59 
     60 复制代码
     61 
     62 
     63 xml数据格式举例
     64 
     65     http://fanyi.youdao.com/openapi.do?keyfrom=<keyfrom>&key=<key>&type=data&doctype=xml&version=1.1&q=这里是有道翻译API  
     66     <?xml version="1.0" encoding="UTF-8"?>  
     67     <youdao-fanyi>  
     68         <errorCode>0</errorCode>  
     69         <!-- 有道翻译 -->  
     70         <query><![CDATA[这里是有道翻译API]]></query>  
     71         <translation>  
     72             <paragraph><![CDATA[Here is the youdao translation API]]></paragraph>  
     73         </translation>  
     74     </youdao-fanyi>  
     75 
     76 复制代码
     77 
     78 
     79 jsonp数据格式举例
     80 
     81     http://fanyi.youdao.com/openapi.do?keyfrom=<keyfrom>&key=<key>&type=data&doctype=jsonp&callback=show&version=1.1&q=API  
     82     show({  
     83         "errorCode":0  
     84         "query":"API",  
     85         "translation":["API"], // 有道翻译  
     86         "basic":{ // 有道词典-基本词典  
     87             "explains":[  
     88                 "abbr. 应用程序界面(Application Program Interface);..."  
     89             ]  
     90         },  
     91         "web":[ // 有道词典-网络释义  
     92             {  
     93                 "key":"API",  
     94                 "value":["应用程序接口(Application Programming Interface)","应用编程接口","应用程序编程接口","美国石油协会"]  
     95             },  
     96             {...}  
     97         ]  
     98     });  
     99 
    100 复制代码
    101 
    102 
    103 具体实现:
    104 
    105     <font size="3" color="#000000" face="微软雅黑">package xiaosi.youdao;  
    106       
    107     import org.apache.http.HttpResponse;  
    108     import org.apache.http.client.methods.HttpGet;  
    109     import org.apache.http.impl.client.DefaultHttpClient;  
    110     import org.apache.http.util.EntityUtils;  
    111     import org.json.JSONArray;  
    112     import org.json.JSONObject;  
    113       
    114     import android.app.Activity;  
    115     import android.os.Bundle;  
    116     import android.view.View;  
    117     import android.view.View.OnClickListener;  
    118     import android.widget.Button;  
    119     import android.widget.EditText;  
    120     import android.widget.TextView;  
    121     import android.widget.Toast;  
    122       
    123     public class YoudaoActivity extends Activity  
    124     {  
    125         private EditText    edit                = null;  
    126         private Button      search              = null;  
    127         private TextView    text                = null;  
    128         private String      YouDaoBaseUrl       = "http://fanyi.youdao.com/openapi.do";  
    129         private String      YouDaoKeyFrom       = "MyLifes";  
    130         private String      YouDaoKey           = "你申请的API Key";  
    131         private String      YouDaoType          = "data";  
    132         private String      YouDaoDoctype       = "json";  
    133         private String      YouDaoVersion       = "1.1";  
    134         @Override  
    135         public void onCreate(Bundle savedInstanceState)  
    136         {  
    137             super.onCreate(savedInstanceState);  
    138             setContentView(R.layout.main);  
    139             init();  
    140         }  
    141       
    142         private void init()  
    143         {  
    144             edit = (EditText) findViewById(R.id.edit);  
    145             search = (Button) findViewById(R.id.search);  
    146             search.setOnClickListener(new searchListener());  
    147             text = (TextView) findViewById(R.id.text);  
    148         }  
    149       
    150         private class searchListener implements OnClickListener  
    151         {  
    152             @Override  
    153             public void onClick(View v)  
    154             {  
    155                 String YouDaoSearchContent = edit.getText().toString().trim();  
    156                 String YouDaoUrl = YouDaoBaseUrl+"?keyfrom=" + YouDaoKeyFrom + "&key=" + YouDaoKey + "&type=" + YouDaoType + "&doctype="  
    157                         + YouDaoDoctype + "&type=" + YouDaoType + "&version=" + YouDaoVersion + "&q=" + YouDaoSearchContent;  
    158                 try  
    159                 {  
    160                     AnalyzingOfJson(YouDaoUrl);  
    161                 }  
    162                 catch (Exception e)  
    163                 {  
    164                     e.printStackTrace();  
    165                 }  
    166             }  
    167         }  
    168       
    169         private void AnalyzingOfJson(String url) throws Exception  
    170         {  
    171             // 第一步,创建HttpGet对象  
    172             HttpGet httpGet = new HttpGet(url);  
    173             // 第二步,使用execute方法发送HTTP GET请求,并返回HttpResponse对象  
    174             HttpResponse httpResponse = new DefaultHttpClient().execute(httpGet);  
    175             if (httpResponse.getStatusLine().getStatusCode() == 200)  
    176             {  
    177                 // 第三步,使用getEntity方法活得返回结果  
    178                 String result = EntityUtils.toString(httpResponse.getEntity());  
    179                 System.out.println("result:" + result);  
    180                 JSONArray jsonArray = new JSONArray("[" + result + "]");  
    181                 String message = null;  
    182                 for (int i = 0; i < jsonArray.length(); i++)  
    183                 {  
    184                     JSONObject jsonObject = jsonArray.getJSONObject(i);  
    185                     if (jsonObject != null)  
    186                     {  
    187                         String errorCode = jsonObject.getString("errorCode");  
    188                         if (errorCode.equals("20"))  
    189                         {  
    190                             Toast.makeText(getApplicationContext(), "要翻译的文本过长", Toast.LENGTH_SHORT);  
    191                         }  
    192                         else if (errorCode.equals("30 "))  
    193                         {  
    194                             Toast.makeText(getApplicationContext(), "无法进行有效的翻译", Toast.LENGTH_SHORT);  
    195                         }  
    196                         else if (errorCode.equals("40"))  
    197                         {  
    198                             Toast.makeText(getApplicationContext(), "不支持的语言类型", Toast.LENGTH_SHORT);  
    199                         }  
    200                         else if (errorCode.equals("50"))  
    201                         {  
    202                             Toast.makeText(getApplicationContext(), "无效的key", Toast.LENGTH_SHORT);  
    203                         }  
    204                         else  
    205                         {  
    206                             // 要翻译的内容  
    207                             String query = jsonObject.getString("query");  
    208                             message = query;  
    209                             // 翻译内容  
    210                             String translation = jsonObject.getString("translation");  
    211                             message += "	" + translation;  
    212                             // 有道词典-基本词典  
    213                             if (jsonObject.has("basic"))  
    214                             {  
    215                                 JSONObject basic = jsonObject.getJSONObject("basic");  
    216                                 if (basic.has("phonetic"))  
    217                                 {  
    218                                     String phonetic = basic.getString("phonetic");  
    219                                     message += "
    	" + phonetic;  
    220                                 }  
    221                                 if (basic.has("phonetic"))  
    222                                 {  
    223                                     String explains = basic.getString("explains");  
    224                                     message += "
    	" + explains;  
    225                                 }  
    226                             }  
    227                             // 有道词典-网络释义  
    228                             if (jsonObject.has("web"))  
    229                             {  
    230                                 String web = jsonObject.getString("web");  
    231                                 JSONArray webString = new JSONArray("[" + web + "]");  
    232                                 message += "
    网络释义:";  
    233                                 JSONArray webArray = webString.getJSONArray(0);  
    234                                 int count = 0;  
    235                                 while(!webArray.isNull(count)){  
    236                                      
    237                                     if (webArray.getJSONObject(count).has("key"))  
    238                                     {  
    239                                         String key = webArray.getJSONObject(count).getString("key");  
    240                                         message += "
    	<"+(count+1)+">" + key;  
    241                                     }  
    242                                     if (webArray.getJSONObject(count).has("value"))  
    243                                     {  
    244                                         String value = webArray.getJSONObject(count).getString("value");  
    245                                         message += "
    	   " + value;  
    246                                     }  
    247                                     count++;  
    248                                 }  
    249                             }  
    250                         }  
    251                     }  
    252                 }  
    253                 text.setText(message);  
    254             }  
    255             else  
    256             {  
    257                 Toast.makeText(getApplicationContext(), "提取异常", Toast.LENGTH_SHORT);  
    258             }  
    259         }  
    260     }  </font>
    261 
    262 复制代码
    263 
    264 
    265 main.xml
    266 
    267     <font size="3" color="#000000" face="微软雅黑">
    268     <?xml version="1.0" encoding="utf-8"?>  
    269     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    270         android:layout_width="fill_parent"  
    271         android:layout_height="fill_parent"  
    272         android:orientation="vertical"   
    273         android:background="@drawable/a">  
    274       
    275         <EditText  
    276             android:id="@+id/edit"  
    277             android:layout_width="fill_parent"  
    278             android:layout_height="wrap_content"  
    279             android:hint="输入你要查询的内容......" />  
    280       
    281         <Button  
    282             android:id="@+id/search"  
    283             android:layout_width="wrap_content"  
    284             android:layout_height="wrap_content"  
    285             android:hint="查询" />  
    286       
    287         <ScrollView  
    288             android:layout_width="fill_parent"  
    289             android:layout_height="wrap_content"  
    290             android:scrollbars="none" >  
    291       
    292             <TextView  
    293                 android:id="@+id/text"  
    294                 android:layout_width="fill_parent"  
    295                 android:layout_height="wrap_content"   
    296                 android:textColor="#000000"/>  
    297         </ScrollView>  
    298       
    299     </LinearLayout>  
    300     </font>
    301 
    302 复制代码

    下面是具体的实现过程:

    1:activity_mian.xml:

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:background="@drawable/a"
     6     android:paddingBottom="@dimen/activity_vertical_margin"
     7     android:paddingLeft="@dimen/activity_horizontal_margin"
     8     android:paddingRight="@dimen/activity_horizontal_margin"
     9     android:paddingTop="@dimen/activity_vertical_margin"
    10     tools:context=".MainActivity" >
    11     <EditText
    12         android:id="@+id/etWord"
    13         android:layout_width="wrap_content"
    14         android:layout_height="wrap_content"
    15         android:layout_alignParentLeft="true"
    16         android:layout_alignParentTop="true"
    17         android:layout_marginTop="27dp"
    18         android:background="@android:drawable/edit_text"
    19         android:ems="10"
    20         android:singleLine="true"
    21         android:textColor="#552006"
    22         android:textColorHint="#782f10" >
    23         <requestFocus />
    24     </EditText>
    25     <WebView
    26         android:id="@+id/wvSearchResult"
    27         android:layout_width="match_parent"
    28         android:layout_height="match_parent"
    29         android:layout_alignLeft="@+id/etWord"
    30         android:layout_below="@+id/etWord"
    31         android:layout_marginTop="22dp"
    32         android:background="@drawable/bg_roundcorner"
    33         android:textAppearance="?android:attr/textAppearanceMedium"
    34         android:textSize="25sp" />
    35 
    36     <Button
    37         android:id="@+id/btnSearch"
    38         android:layout_width="wrap_content"
    39         android:layout_height="wrap_content"
    40         android:layout_above="@+id/wvSearchResult"
    41         android:layout_alignParentRight="true"
    42         android:background="@drawable/ibsearchword"
    43         android:onClick="searchWord" />
    44 
    45 </RelativeLayout>

    2:MainActivity:

     1 package com.example.youdaodictionary;
     2 
     3 import android.app.Activity;
     4 import android.os.Bundle;
     5 import android.text.TextUtils;
     6 import android.view.Menu;
     7 import android.view.View;
     8 import android.webkit.WebView;
     9 import android.webkit.WebViewClient;
    10 import android.widget.EditText;
    11 import android.widget.Toast;
    12 
    13 public class MainActivity extends Activity {
    14     private EditText etWord;
    15     private WebView wvResult;
    16 
    17     @Override
    18     protected void onCreate(Bundle savedInstanceState) {
    19         super.onCreate(savedInstanceState);
    20         setContentView(R.layout.activity_main);
    21 
    22         initViews();
    23     }
    24 
    25     private void initViews() {
    26         etWord = (EditText) findViewById(R.id.etWord);
    27         wvResult = (WebView) findViewById(R.id.wvSearchResult);
    28         wvResult.setWebViewClient(new WebViewClient() {
    29             @Override
    30             public boolean shouldOverrideUrlLoading(WebView view, String url) {
    31                 view.loadUrl(url);
    32                 return true;
    33             }
    34         });
    35     }
    36 
    37     @Override
    38     public boolean onCreateOptionsMenu(Menu menu) {
    39         getMenuInflater().inflate(R.menu.main, menu);
    40         return true;
    41     }
    42 
    43     public void searchWord(View view) {
    44         String word = etWord.getText().toString();
    45         if (TextUtils.isEmpty(word)) {
    46             Toast.makeText(this, "查询内容不能为空!", Toast.LENGTH_LONG).show();
    47         } else {
    48 
    49             final String strUrl = "http://dict.youdao.com/m/search?keyfrom=dict.mindex&vendor=&q="
    50                     + word;
    51             wvResult.loadUrl(strUrl);
    52         }
    53     }
    54 }

    3:界面实现:

       每个人的心里都有一个伟大的梦想,不用告诉别人,自己放在心里,默默的为之努力就好。

      有些路很远,走下去会很累,不走,会后悔。

     

     

     

     

    这世界上有一种鸟是没有脚的,它只能够一直的飞呀飞呀,飞累了就在风里面睡觉,这种鸟一辈子只能下地一次,那一次就是它死亡的时候。
  • 相关阅读:
    .NET WinForm 状态栏添加分隔符
    使用 MMC / IE 查看证书
    配置IIS在64位Windows上运行 32 位 ASP.NET 应用程序
    WCF部署到IIS:证书必须具有能够进行密钥交换的私钥,该进程必须具有访问私钥的权限
    PB6 调用 .net Web Service
    .NET程序加壳 — 之动态加载程序集
    statusStrip 状态条 toolStripStatusLabel 居右显示
    C# 使用 Stopwatch 测量代码运行时间
    Application_Start 事件中使用 Response.Redirect
    解决IIS中部署WCF时,访问.svc文件的404错误问题
  • 原文地址:https://www.cnblogs.com/xuyinghui/p/4587099.html
Copyright © 2020-2023  润新知