欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新

利用百度词典API和Volley网络库开发的android词典应用,volleyandroid

来源: 开发者 投稿于  被查看 934 次 评论:199

利用百度词典API和Volley网络库开发的android词典应用,volleyandroid


   关于百度词典API的说明,地址在这里:百度词典API介绍

  关于android网络库Volley的介绍说明,地址在这里:Android网络通信库Volley

  首先我们看下大体的界面布局!

  

  再帖张最终的效果图

  

  看到效果图,应该知道布局很简单了对吧:

  布局xml文件如下:

<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>

 

 

  然后就是几个事件的处理,

  1: Clear按钮的处理: 清理掉输入框里面的内容

  2: Query按钮的处理:

    1:利用百度词典API获取内容

    2:解析返回的json数据,处理并且显示在下面的TextView空间中,

    3:取消软键盘的显示

  代码如下:

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.ActionBarActivity; 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

  

  需要改进的地方:

    1:做一个本地查询记录的存储

    2:解析内容做一个Scroll的控制

用户评论