about the Baidu Dictionary API description, address here: Baidu Dictionary API Introduction
For an introduction to the Android Network library volley, address here: Android network communication library volley
First we look at the general layout of the interface!
and the final one.
See, should know the layout is very simple, right:
The layout XML file is as follows:
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"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= "Com.example.dict.MainActivity" > <LinearLayoutandroid:orientation= "Horizontal"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content" > <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Clear"Android:onclick= "Clearhandler" /> <EditTextAndroid:id= "@+id/etword"Android:singleline= "true"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "2" /> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Query"Android:onclick= "Queryhandler" /> </LinearLayout> <TextViewAndroid:id= "@+id/tvresult"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:hint= "Result" /></LinearLayout>
View Code
Then there are the handling of several events,
1:clear button Processing: Clean out the contents of the input box
2:query button Handling:
1: Using the Baidu Dictionary API to get content
2: Parse the returned JSON data, process and display in the TextView space below,
3: Cancel display of soft keyboard
The code is as follows:
Packagecom.example.dict;ImportJava.util.HashMap;ImportJava.util.Map;ImportOrg.json.JSONArray;ImportOrg.json.JSONObject;Importcom.android.volley.Request;ImportCom.android.volley.RequestQueue;ImportCom.android.volley.Response;ImportCom.android.volley.VolleyError;Importcom.android.volley.toolbox.StringRequest;ImportCom.android.volley.toolbox.Volley;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.content.Context;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.inputmethod.InputMethodManager;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView; Public classMainactivityextendsactionbaractivity {PrivateString url = "http://openapi.baidu.com/public/2.0/translate/dict/simple?client_id=5kHZHeo8MN7L6NmPTGV6POsb&q= @word &from=en&to=zh "; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); } Public voidClearhandler (view view) {EditText text=(EditText) Findviewbyid (R.id.etword); Text.settext (""); } Public voidQueryhandler (view view) {EditText text=(EditText) Findviewbyid (R.id.etword); if(Text.gettext (). Length () <= 0) { return ; } requestqueue requestqueue= Volley.newrequestqueue ( This); String Tempurl= This. Url.replace ("@word", Text.gettext (). toString ()); Stringrequest postrequest=Newstringrequest (Request.method.get,tempurl,NewResponse.listener<string>() {@Override Public voidOnresponse (String response) {mainactivity. This. Parseresult (response); } }, NewResponse.errorlistener () {@Override Public voidonerrorresponse (volleyerror error) {}} ) {@OverrideprotectedMap<string,string>Getparams () {Map<String,String> params =NewHashmap<string,string>(); returnparams; }}; Requestqueue.add (postrequest); } Public voidParseresult (String source) {Try { FinalTextView display =(TextView) Findviewbyid (R.id.tvresult); Display.settext (""); Inputmethodmanager IMM=(Inputmethodmanager) Getsystemservice (Context.input_method_service); if(Imm.isactive ()) {Imm.togglesoftinput (inputmethodmanager.show_implicit, Inputmethodmanage R.hide_not_always); } jsonobject obj=Newjsonobject (source); Jsonobject Data= Obj.getjsonobject ("Data"); Jsonarray symbols= Data.getjsonarray ("Symbols"); for(intI=0;i<symbols.length (); i++) {Jsonobject parts=Symbols.getjsonobject (i); for(intJ=0;j < Parts.getjsonarray ("parts"). Length (); J + +) {Jsonobject Item= Parts.getjsonarray ("Parts"). Getjsonobject (j); Display.append (Item.getstring ("Part")); Display.append ("\ n"); Jsonarray arr= Item.getjsonarray ("means"); for(intK=0;k<arr.length (); k++) {display.append (" " +arr.getstring (k)); Display.append ("\ n"); } } } } Catch(Exception e) {} }}View Code
Areas needing improvement:
1: Make a local query record storage
2: Parse content to do a scroll control
Android Dictionary application developed using the Baidu Dictionary API and the Volley network library