码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-02-16 01:38:47      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:

   关于百度词典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="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Clear"
            android:onClick="clearHandler"
            />
        <EditText
            android:id="@+id/etWord"
            android:singleLine="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
             />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Query"
            android:onClick="queryHandler"
             />
    </LinearLayout>
    <TextView 
        android:id="@+id/tvResult"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:hint="result"
        />

</LinearLayout>
View Code

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

  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的控制

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

标签:

原文地址:http://www.cnblogs.com/archy_yu/p/4293635.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!