Android dictionary application developed using Baidu dictionary API and Volley network library, volleyandroid

Source: Internet
Author: User

Android dictionary application developed using Baidu dictionary API and Volley network library, volleyandroid

For more information about Baidu dictionary API, visit: Baidu dictionary API introduction.

Here is an introduction to the android network library Volley.

First, let's take a look at the general interface layout!

  

Repost the final

  

We can see that the layout is very simple, right:

The layout xml file is as follows:

<LinearLayout xmlns: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" >          <LinearLayout         android:orientation="horizontal"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        >                <Button             android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="Clear"            android:onClick="clearHandler"            android:padding="7dip"            />        <EditText            android:id="@+id/etWord"            android:singleLine="true"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="2"            android:background="@android:drawable/edit_text"             />        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="Query"            android:onClick="queryHandler"            android:padding="7dip"             />    </LinearLayout>    <TextView         android:id="@+id/tvResult"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:hint="result"        /></LinearLayout>

 

 

Then there are several events,

1: Clear button processing: Clear the content in the input box

2: Query button processing:

1: Use the Baidu dictionary API to retrieve content

2: parse the returned json data and process it and display it in the TextView space below,

3: cancel the display of the keyboard.

The Code is as follows:

Package com. example. dict; import java. util. hashMap; import java. util. map; import org. json. JSONArray; import org. json. JSONObject; import com. android. volley. request; import com. android. volley. requestQueue; import com. android. volley. response; import com. android. volley. volleyError; import com. android. volley. toolbox. stringRequest; import com. android. volley. toolbox. volley; import android. support. v7.app. actionBarAct Ivity; import android. content. context; import android. OS. bundle; import android. view. view; import android. view. inputmethod. inputMethodManager; import android. widget. editText; import android. widget. textView; public class MainActivity extends ActionBarActivity {private String url = "http://openapi.baidu.com/public/2.0/translate/dict/simple? Client_id = 5kHZHeo8MN7L6NmPTGV6POsb & q = @ word & from = en & to = zh "; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public void clearHandler (View view) {EditText text = (EditText) findViewById (R. id. etWord); text. setText ("");} public void queryHandler (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 = new StringRequest (Request. method. GET, tempUrl, new Response. listener <String> () {@ Override public void onResponse (String response) {MainActivity. this. parseResult (response) ;}}, new Response. errorListener () {@ Override public void onErrorResponse (VolleyError error) {}}) {@ Override protected Map <String, String> getParams () {Map <String, string> params = new HashMap <String, String> (); return params ;}}; requestQueue. add (postRequest);} public void parseResult (String source) {try {final TextView display = (TextView) findViewById (R. id. tvResult); display. setText (""); InputMethodManager imm = (InputMethodManager) getSystemService (Context. INPUT_METHOD_SERVICE); if (imm. isActive () {imm. toggleSoftInput (InputMethodManager. SHOW_IMPLICIT, InputMethodManager. HIDE_NOT_ALWAYS);} JSONObject obj = new JSONObject (source); JSONObject data = obj. getJSONObject ("data"); JSONArray symbols = data. getJSONArray ("symbols"); for (int I = 0; I <symbols. length (); I ++) {JSONObject parts = symbols. getJSONObject (I); for (int j = 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 (int k = 0; k <arr. length (); k ++) {display. append ("" + arr. getString (k); display. append ("\ n") ;}}} catch (Exception e ){}}}View Code

  

Areas for improvement:

1: store a local query record

2: Perform Scroll control on the parsed content

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.