Android進階2之有道詞典開發

來源:互聯網
上載者:User

本博文只是實現有道詞典的功能,並著重介面。

首先,你需要擷取有道開發平台的API key。點擊開啟連結 申請一個吧。

利用資料介面擷取資料:

 

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=翻譯<br />{<br /> "errorCode":0<br /> "query":"翻譯",<br /> "translation":["translation"], // 有道翻譯<br /> "basic":{ // 有道詞典-基本詞典<br /> "phonetic":"fān yì",<br /> "explains":[<br /> "translate",<br /> "interpret"<br /> ]<br /> },<br /> "web":[ // 有道詞典-網路釋義<br /> {<br /> "key":"翻譯",<br /> "value":["translator","translation","translate","Interpreter"]<br /> },<br /> {...}<br /> ]<br />}

xml資料格式舉例

http://fanyi.youdao.com/openapi.do?keyfrom=<keyfrom>&key=<key>&type=data&doctype=xml&version=1.1&q=這裡是有道翻譯API<br /><?xml version="1.0" encoding="UTF-8"?><br /><youdao-fanyi><br /> <errorCode>0</errorCode><br /> <!-- 有道翻譯 --><br /> <query><![CDATA[這裡是有道翻譯API]]></query><br /> <translation><br /> <paragraph><![CDATA[Here is the youdao translation API]]></paragraph><br /> </translation><br /></youdao-fanyi>

 

jsonp資料格式舉例

http://fanyi.youdao.com/openapi.do?keyfrom=<keyfrom>&key=<key>&type=data&doctype=jsonp&callback=show&version=1.1&q=API<br />show({<br /> "errorCode":0<br /> "query":"API",<br /> "translation":["API"], // 有道翻譯<br /> "basic":{ // 有道詞典-基本詞典<br /> "explains":[<br /> "abbr. 應用程式介面(Application Program Interface);..."<br /> ]<br /> },<br /> "web":[ // 有道詞典-網路釋義<br /> {<br /> "key":"API",<br /> "value":["應用程式介面(Application Programming Interface)","應用編程介面","API","美國石油協會"]<br /> },<br /> {...}<br /> ]<br />});
具體實現:

 

 

package xiaosi.youdao;</p><p>import org.apache.http.HttpResponse;<br />import org.apache.http.client.methods.HttpGet;<br />import org.apache.http.impl.client.DefaultHttpClient;<br />import org.apache.http.util.EntityUtils;<br />import org.json.JSONArray;<br />import org.json.JSONObject;</p><p>import android.app.Activity;<br />import android.os.Bundle;<br />import android.view.View;<br />import android.view.View.OnClickListener;<br />import android.widget.Button;<br />import android.widget.EditText;<br />import android.widget.TextView;<br />import android.widget.Toast;</p><p>public class YoudaoActivity extends Activity<br />{<br />private EditTextedit= null;<br />private Buttonsearch= null;<br />private TextViewtext= null;<br />private StringYouDaoBaseUrl= "http://fanyi.youdao.com/openapi.do";<br />private StringYouDaoKeyFrom= "MyLifes";<br />private StringYouDaoKey= "你申請的API Key";<br />private StringYouDaoType= "data";<br />private StringYouDaoDoctype= "json";<br />private StringYouDaoVersion= "1.1";<br />@Override<br />public void onCreate(Bundle savedInstanceState)<br />{<br />super.onCreate(savedInstanceState);<br />setContentView(R.layout.main);<br />init();<br />}</p><p>private void init()<br />{<br />edit = (EditText) findViewById(R.id.edit);<br />search = (Button) findViewById(R.id.search);<br />search.setOnClickListener(new searchListener());<br />text = (TextView) findViewById(R.id.text);<br />}</p><p>private class searchListener implements OnClickListener<br />{<br />@Override<br />public void onClick(View v)<br />{<br />String YouDaoSearchContent = edit.getText().toString().trim();<br />String YouDaoUrl = YouDaoBaseUrl+"?keyfrom=" + YouDaoKeyFrom + "&key=" + YouDaoKey + "&type=" + YouDaoType + "&doctype="<br />+ YouDaoDoctype + "&type=" + YouDaoType + "&version=" + YouDaoVersion + "&q=" + YouDaoSearchContent;<br />try<br />{<br />AnalyzingOfJson(YouDaoUrl);<br />}<br />catch (Exception e)<br />{<br />e.printStackTrace();<br />}<br />}<br />}</p><p>private void AnalyzingOfJson(String url) throws Exception<br />{<br />// 第一步,建立HttpGet對象<br />HttpGet httpGet = new HttpGet(url);<br />// 第二步,使用execute方法發送HTTP GET請求,並返回HttpResponse對象<br />HttpResponse httpResponse = new DefaultHttpClient().execute(httpGet);<br />if (httpResponse.getStatusLine().getStatusCode() == 200)<br />{<br />// 第三步,使用getEntity方法活得返回結果<br />String result = EntityUtils.toString(httpResponse.getEntity());<br />System.out.println("result:" + result);<br />JSONArray jsonArray = new JSONArray("[" + result + "]");<br />String message = null;<br />for (int i = 0; i < jsonArray.length(); i++)<br />{<br />JSONObject jsonObject = jsonArray.getJSONObject(i);<br />if (jsonObject != null)<br />{<br />String errorCode = jsonObject.getString("errorCode");<br />if (errorCode.equals("20"))<br />{<br />Toast.makeText(getApplicationContext(), "要翻譯的文本過長", Toast.LENGTH_SHORT);<br />}<br />else if (errorCode.equals("30 "))<br />{<br />Toast.makeText(getApplicationContext(), "無法進行有效翻譯", Toast.LENGTH_SHORT);<br />}<br />else if (errorCode.equals("40"))<br />{<br />Toast.makeText(getApplicationContext(), "不支援的語言類型", Toast.LENGTH_SHORT);<br />}<br />else if (errorCode.equals("50"))<br />{<br />Toast.makeText(getApplicationContext(), "無效的key", Toast.LENGTH_SHORT);<br />}<br />else<br />{<br />// 要翻譯的內容<br />String query = jsonObject.getString("query");<br />message = query;<br />// 翻譯內容<br />String translation = jsonObject.getString("translation");<br />message += "\t" + translation;<br />// 有道詞典-基本詞典<br />if (jsonObject.has("basic"))<br />{<br />JSONObject basic = jsonObject.getJSONObject("basic");<br />if (basic.has("phonetic"))<br />{<br />String phonetic = basic.getString("phonetic");<br />message += "\n\t" + phonetic;<br />}<br />if (basic.has("phonetic"))<br />{<br />String explains = basic.getString("explains");<br />message += "\n\t" + explains;<br />}<br />}<br />// 有道詞典-網路釋義<br />if (jsonObject.has("web"))<br />{<br />String web = jsonObject.getString("web");<br />JSONArray webString = new JSONArray("[" + web + "]");<br />message += "\n網路釋義:";<br />JSONArray webArray = webString.getJSONArray(0);<br />int count = 0;<br />while(!webArray.isNull(count)){</p><p>if (webArray.getJSONObject(count).has("key"))<br />{<br />String key = webArray.getJSONObject(count).getString("key");<br />message += "\n\t<"+(count+1)+">" + key;<br />}<br />if (webArray.getJSONObject(count).has("value"))<br />{<br />String value = webArray.getJSONObject(count).getString("value");<br />message += "\n\t " + value;<br />}<br />count++;<br />}<br />}<br />}<br />}<br />}<br />text.setText(message);<br />}<br />else<br />{<br />Toast.makeText(getApplicationContext(), "提取異常", Toast.LENGTH_SHORT);<br />}<br />}<br />}<br />
main.xml

 

 

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> android:orientation="vertical"<br /> android:background="@drawable/a"></p><p> <EditText<br /> android:id="@+id/edit"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:hint="輸入你要查詢的內容......" /></p><p> <Button<br /> android:id="@+id/search"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:hint="查詢" /></p><p> <ScrollView<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:scrollbars="none" ></p><p> <TextView<br /> android:id="@+id/text"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:textColor="#000000"/><br /> </ScrollView></p><p></LinearLayout>

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.