有道词典

1.实例(在EditText中输入查询的英文或中文,结果在下方显示)

2.MainActivity.java 代码如下:


 package com.example.youdaodictionary;

  importandroid.app.Activity;

  importandroid.os.Bundle;

  importandroid.text.TextUtils;

  import android.view.Menu;

  importandroid.view.View;

  importandroid.webkit.WebView;

  importandroid.webkit.WebViewClient;

 import android.widget.EditText;

 import android.widget.Toast;

 public class MainActivity extendsActivity {

    private EditText etWord;

     private WebView wvResult; 

    @Override

    protected void onCreate(BundlesavedInstanceState) {

        super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main); 

        initViews();

    } 

    private void initViews() {

        etWord = (EditText) findViewById(R.id.etWord);

        wvResult = (WebView) findViewById(R.id.wvSearchResult);

        wvResult.setWebViewClient(new WebViewClient() {

             @Override

             public booleanshouldOverrideUrlLoading(WebView view, String url) {

                 view.loadUrl(url);

                return true;

             }

        });

    }

 

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

         getMenuInflater().inflate(R.menu.main,menu);

        return true;

    } 

    public void searchWord(Viewview) {

        String word = etWord.getText().toString();

        if (TextUtils.isEmpty(word)) {

             Toast.makeText(this, "查询内容不能为空!",Toast.LENGTH_LONG).show();

        } else {

             final String strUrl = http://dict.youdao.com/m/search?keyfrom=dict.mindex&vendor=&q=

                     + word;

             wvResult.loadUrl(strUrl);

        }

    }

activity.xml:



 <RelativeLayoutxmlns: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:background="@drawable/a"

     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=".MainActivity" >

    <EditText

         android:id="@+id/etWord"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:layout_alignParentLeft="true"

         android:layout_alignParentTop="true"

         android:layout_marginTop="27dp"

         android:background="@android:drawable/edit_text"

         android:ems="10"

         android:singleLine="true"

         android:textColor="#552006"

         android:textColorHint="#782f10" >

         <requestFocus />

     </EditText>

    <WebView

         android:id="@+id/wvSearchResult"

         android:layout_width="match_parent"

         android:layout_height="match_parent"

         android:layout_alignLeft="@+id/etWord"

         android:layout_below="@+id/etWord"

         android:layout_marginTop="22dp"

         android:background="@drawable/bg_roundcorner"

         android:textAppearance="?android:attr/textAppearanceMedium"

         android:textSize="25sp" />

    <Button

         android:id="@+id/btnSearch"

        android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:layout_above="@+id/wvSearchResult"

         android:layout_alignParentRight="true"

         android:background="@drawable/ibsearchword"

         android:onClick="searchWord" />

 </RelativeLayout>



jsonp数据格式举例

     http://fanyi.youdao.com/openapi.do?keyfrom=<keyfrom>&key=<key>&type=data&doctype=jsonp&callback=show&version=1.1&q=API 

     show({ 

         "errorCode":0 

          "query":"API", 

         "translation":["API"], // 有道翻译 

         "basic":{ // 有道词典-基本词典 

              "explains":[ 

                  "abbr. 应用程序界面(Application Program Interface);..." 

              ] 

          }, 

          "web":[ // 有道词典-网络释义 

              { 

                  "key":"API", 

                  "value":["应用程序接口(ApplicationProgramming Interface)","应用编程接口","应用程序编程接口","美国石油协会"] 

              }, 

              {...} 

          ] 

      });   

 具体实现: 

    <font size="3" color="#000000" face="微软雅黑">packagexiaosi.youdao;       

    import org.apache.http.HttpResponse; 

    import org.apache.http.client.methods.HttpGet; 

     importorg.apache.http.impl.client.DefaultHttpClient; 

     importorg.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 YoudaoActivityextends 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       = "MyLifes"; 

        private String      YouDaoKey           = "你申请的API Key"; 

        private String      YouDaoType          = "data"; 

        private String      YouDaoDoctype       = "json"; 

        private String      YouDaoVersion       = "1.1"; 

        @Override 

        public void onCreate(BundlesavedInstanceState) 

        { 

           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(newsearchListener()); 

             text = (TextView)findViewById(R.id.text); 

        } 

      

        private class searchListenerimplements 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 voidAnalyzingOfJson(String url) throws Exception 

        { 

             // 第一步,创建HttpGet对象 

             HttpGet httpGet = new HttpGet(url); 

             // 第二步,使用execute方法发送HTTP GET请求,并返回HttpResponse对象 

             HttpResponse httpResponse = newDefaultHttpClient().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")) 

                             { 

                                 JSONObjectbasic = jsonObject.getJSONObject("basic"); 

                                 if (basic.has("phonetic")) 

                                 { 

                                     Stringphonetic = basic.getString("phonetic"); 

                                     message += "\n\t" + phonetic; 

                                 } 

                                 if (basic.has("phonetic")) 

                                 { 

                                     Stringexplains = basic.getString("explains"); 

                                     message +="\n\t" + explains; 

                                 } 

                             } 

                             // 有道词典-网络释义 

                             if (jsonObject.has("web")) 

                             { 

                                 String web =jsonObject.getString("web"); 

                                 JSONArraywebString = new JSONArray("[" + web + "]"); 

                                 message += "\n网络释义:"; 

                                 JSONArraywebArray = webString.getJSONArray(0); 

                                int count = 0; 

                                 while(!webArray.isNull(count)){ 

                                     if(webArray.getJSONObject(count).has("key")) 

                                     { 

                                         Stringkey = webArray.getJSONObject(count).getString("key");                                          message += "\n\t<"+(count+1)+">" + key; 

                                     } 

                                     if(webArray.getJSONObject(count).has("value")) 

                                     { 

                                         Stringvalue = webArray.getJSONObject(count).getString("value"); 

                                        message += "\n\t   " + value; 

                                    } 

                                   count++; 

                                 } 

                             } 

                         } 

                     } 

                 } 

                 text.setText(message); 

             } 

             else 

             { 

                Toast.makeText(getApplicationContext(), "提取异常",Toast.LENGTH_SHORT); 

             } 

        } 

     } </font>

 


 


 main.xml 

     <font size="3" color="#000000" face="微软雅黑">

     <?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> 

     </font>

 


 



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值