Android advanced 2 youdao dictionary Development

Source: Internet
Author: User
Tags cdata unsupported

This blog post only implements the feature of youdao dictionary and focuses on the interface.

First, you need to obtain the API key of youdao development platform. Click Open link to apply for one.

Use the data interface to obtain data:

 

Http://fanyi.youdao.com/openapi.do? Keyfrom = <keyfrom> & Key = <key> & type = Data & doctype = <doctype> & version = 1.1 & Q = text to be translated

Version: 1.1, request method: Get, encoding method: UTF-8

Main functions: English-Chinese translation, and obtaining the results of youdao translation and youdao Dictionary (may not)

Parameter description:

Type-type of the returned result, fixed to data

Doctype-data format of the returned results, XML, JSON, or jsonp

Version-version. The latest version is 1.1.

Q-the text to be translated cannot exceed 200 characters and must be UTF-8 encoded

Errorcode:

0-normal

20-the text to be translated is too long

30-no valid translation is available

40-unsupported language types

50-invalid key

Returned sample:

 

Example of JSON Data Format

Http://fanyi.youdao.com/openapi.do? Keyfrom = <keyfrom> & Key = <key> & type = Data & doctype = JSON & version = 1.1 & Q = translation <br/>{< br/> "errorcode ": 0 <br/> "query": "Translation", <br/> "Translation": ["Translation"], // youdao translation <br/> "Basic ": {// youdao dictionary-basic dictionary <br/> "phonetic": "f limit n y limit", <br/> "explains ": [<br/> "translate", <br/> "interpret" <br/>] <br/>}, <br/> "Web ": [// youdao dictionary-network definition <br/>{< br/> "key": "Translation", <br/> "value": ["translator ", "Translation", "translate", "interpreter"] <br/>}, <br/> {...} <br/>] <br/>}

XML data format example

Http://fanyi.youdao.com/openapi.do? Keyfrom = <keyfrom> & Key = <key> & type = Data & doctype = xml & version = 1.1 & Q = here is the youdao translation API <br/> <? XML version = "1.0" encoding = "UTF-8"?> <Br/> <youdao-Fanyi> <br/> <errorcode> 0 </errorcode> <br/> <! -- Youdao Translation --> <br/> <query> <! [CDATA [API]> </query> <br/> <translation> <br/> <Paragraph> <! [CDATA [here is the youdao translation API]> </Paragraph> <br/> </translation> <br/> </youdao-Fanyi>

 

Example of jsonp Data Format

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"], // youdao translation <br/> "Basic ": {// youdao dictionary-basic dictionary <br/> "explains": [<br/> "abbr. application program interface );... "<br/>] <br/>}, <br/>" Web ": [// youdao dictionary-network definition <br/>{< br/> "key": "API", <br/> "value ": ["Application Programming Interface", "American Petroleum Association"] <br/> }, <br/> {...} <br/>] <br/> });
Specific implementation:

 

 

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 Oid. 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 =" the API key you applied for "; <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/>}</P> <p> private void analyzingofjson (string URL) throws exception <br/> {<br // Step 1: Create an httpget object <br/> httpget = new httpget (URL); <br/> // Step 2: Use the execute method to send an http get request, and return the httpresponse object <br/> httpresponse = new defaulthttpclient(.exe cute (httpget); <br/> If (httpresponse. getstatusline (). getstatuscode () = 200) <br/>{< br/> // Step 3: Use the getentity method to return results. <br/> string result = entityutils. tostring (httpresponse. getentity (); <br/> system. out. println ("Result:" + Res Ult); <br/> jsonarray = new jsonarray ("[" + Result + "]"); <br/> string message = NULL; <br/> for (INT I = 0; I <jsonarray. length (); I ++) <br/>{< br/> 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 (), "the text to be translated is too long", toast. length_short); <br/>}< br/> else if (errorcode. equals ("30") <br/>{< br/> toast. maketext (getapplicationcontext (), "cannot translate effectively", toast. length_short); <br/>}< br/> else if (errorcode. equals ("40") <br/>{< br/> toast. maketext (getap Plicationcontext (), "unsupported language type", toast. length_short); <br/>}< br/> else if (errorcode. equals ("50") <br/>{< br/> toast. maketext (getapplicationcontext (), "invalid key", toast. length_short); <br/>}< br/> else <br/>{< br/> // content to be translated <br/> string query = jsonobject. getstring ("query"); <br/> message = query; <br/> // translation content <br/> string translation = jsonobject. getstring ("Translation"); <br/> message + = "\ t" + translat Ion; <br/> // youdao dictionary-basic dictionary <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/> // Youdao dictionary-network definition <br/> If (jsonobject. has ("Web") <br/>{< br/> string web = jsonobject. getstring ("Web"); <br/> jsonarray webstring = new jsonarray ("[" + Web + "]"); <br/> message + = "\ n network definition:"; <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/> text. settext (Message); <br/>}< br/> else <br/>{< br/> toast. maketext (getapplicationcontext (), "extraction exception", toast. length_short); <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 = "Enter the content you want to query ...... "/> </P> <p> <button <br/> Android: Id =" @ + ID/search "<br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: hint = "query"/> </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>

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.