Android Notes (53) Use Youdao Openapi to do simple translation demo

Source: Internet
Author: User

Go to Http://fanyi.youdao.com/openapi?path=data-mode to apply for developer key first

Youdao API will automatically translate and return the requested Word to XML or JSON format, all we need to do is to display the returned format to the screen.

Mainactivity.java

 Packagecn.lixyz.youdaodictionary;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;Importjava.net.MalformedURLException;ImportJava.net.URL;Importjava.net.URLConnection;ImportOrg.json.JSONArray;Importorg.json.JSONException;ImportOrg.json.JSONObject;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {PrivateEditText Word; PrivateButton submit; PrivateTextView translation; PrivateString str = "http://fanyi.youdao.com/openapi.do?keyfrom=androidhttptest&key=507293865&type=data& Doctype=json&version=1.1&q= ";    String Requeststr; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Initview (); Submit.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {String Tmpword=Word.gettext (). toString (). Trim (); Requeststr= str +Tmpword; NewThread (NewRunnable () {@Override Public voidrun () {URL url; Try{URL=NewURL (REQUESTSTR); URLConnection Connection=url.openconnection (); InputStream is=Connection.getinputstream (); InputStreamReader ISR=NewInputStreamReader (IS, "UTF-8"); BufferedReader BR=NewBufferedReader (ISR); String Tmpline= ""; String Line=NULL;  while(line = Br.readline ())! =NULL) {Tmpline= Tmpline +Line ; } Message msg=NewMessage (); Msg.obj=Tmpline;                        Handler.sendmessage (msg); } Catch(malformedurlexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace ();            }}). Start ();    }        }); }    Private voidInitview () {Word=(EditText) Findviewbyid (R.id.word); Submit=(Button) Findviewbyid (r.id.submit); Translation=(TextView) Findviewbyid (r.id.translation); } Handler Handler=NewHandler () { Public voidhandlemessage (android.os.Message msg) {//Translation.settext (msg.obj.toString ());            Try{Jsonobject Object=NewJsonobject (msg.obj.toString ()); Jsonobject Jsonobject= Object.getjsonobject ("Basic"); String usphonetic= Jsonobject.getstring ("us-phonetic"); String Phonetic= jsonobject.getstring ("phonetic"); String ukphonetic= Jsonobject.getstring ("uk-phonetic"); Jsonarray Array= Jsonobject.getjsonarray ("explains"); String Showtext= "";  for(inti = 0; I < Array.Length (); i++) {String obj=array.getstring (i); Showtext= Showtext + obj + "\ n"; } Showtext= "American tone: \ t" + usphonetic + "\ n" + "English: \ T" + ukphonetic + "\ n" + "\ n" +Showtext;            Translation.settext (Showtext); } Catch(jsonexception e) {//TODO auto-generated Catch blockE.printstacktrace ();    }        }; };}

Activity_main.xml

<LinearLayoutxmlns: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:orientation= "vertical"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= "Cn.lixyz.youdaodictionary.MainActivity" >    <EditTextAndroid:id= "@+id/word"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_margin= "10DP" />    <ButtonAndroid:id= "@+id/submit"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_margin= "10DP"Android:text= "Translation" />    <TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_margin= "10DP"Android:text= "translated as:" />    <TextViewAndroid:id= "@+id/translation"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_margin= "10DP" /></LinearLayout>

You need to add Intenet permissions because you want to request a network connection

<android:name= "Android.permission.INTERNET"/>

Android Notes (53) Use Youdao Openapi to do simple translation demo

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.