«

Android实战--英文词典(API+GSON+Volley)

时间:2024-3-2 17:45     作者:韩俊     分类: Android


转载请注明出处,喜欢我的可以关注我!

上一节我们介绍了GSON和Volley,用GSON对返回的数据进行了初步解析,这一节我们更进一步,讨论一下如何实现英文词典。首先把JSON数据的样式再看一下:

下面创建bean,这些都和上一节一样:

第一个bean:

[java] view
plaincopy

package org.lxh.demo;

import java.util.List;

public class Status1 {
private int errNum;
private String errMsg;
RetData2 retData;//保证变量名与返回JSON的标签名一致,注意!!!!!!!!
@Override
public String toString() {
return "Status [errNum=" + errNum + ", errMsg=" + errMsg + ", retData="
+ retData + "]";
}
public int getErrNum() {
return errNum;
}
public void setErrNum(int errNum) {
this.errNum = errNum;
}
public String getErrMsg() {
return errMsg;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
public RetData2 getRetData() {
return retData;
}
public void setRetData(RetData2 retData) {
this.retData = retData;
}

}

第二个bean:

[java] view
plaincopy

package org.lxh.demo;

import java.util.List;

public class RetData2 {
private String from;
private String to;
DictResult3 dict_result;
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public DictResult3 getDictResult() {
return dict_result;
}
public void setDictResult(DictResult3 dictResult) {
this.dict_result = dictResult;
}
@Override
public String toString() {
return "RetData [from=" + from + ", to=" + to + ", dictResult="
+ dict_result + "]";
}

}


第3个bean:

[java] view
plaincopy

package org.lxh.demo;

import java.util.List;

public class DictResult3 {
private String word_name;

private List<Symbols> symbols;  

public String getWord_name() {  
    return word_name;  
}  

public void setWord_name(String word_name) {  
    this.word_name = word_name;  
}  

public List<Symbols> getSymbols() {  
    return symbols;  
}  

public void setSymbols(List<Symbols> symbols) {  
    this.symbols = symbols;  
}  

@Override  
public String toString() {  
    return "DictResult [word_name=" + word_name + ", symbols=" + symbols  
            + "]";  
}  

}


第4个bean:


[java] view
plaincopy

package org.lxh.demo;

import java.util.List;

public class Symbols {
private String ph_am;
private String ph_en;
private List<Parts> parts;

public String getPh_am() {  
    return ph_am;  
}  
public void setPh_am(String ph_am) {  
    this.ph_am = ph_am;  
}  
public String getPh_en() {  
    return ph_en;  
}  
public void setPh_en(String ph_en) {  
    this.ph_en = ph_en;  
}  
public List&lt;Parts&gt; getParts() {  
    return parts;  
}  
public void setParts(List&lt;Parts&gt; parts) {  
    this.parts = parts;  
}  
@Override  
public String toString() {  
    return &quot;Symbols [ph_am=&quot; &#43; ph_am &#43; &quot;, ph_en=&quot; &#43; ph_en &#43; &quot;, parts=&quot;  
            &#43; parts &#43; &quot;]&quot;;  
}  

}


第5个:

[java] view
plaincopy

package org.lxh.demo;

import java.util.Arrays;
import java.util.List;

public class Parts {
private String part;
public String getParts() {
return part;
}
public void setParts(String parts) {
this.part = parts;
}
public String[] getMeans() {
return means;
}
public void setMeans(String[] means) {
this.means = means;
}
@Override
public String toString() {
return "Parts [parts=" + part + ", means=" + Arrays.toString(means)
+ "]";
}
String[] means;

}


所需要的jar包也和上一节一样,可以参考上一节,我们主要讨论一下MainActivity.java程序:

package org.lxh.demo;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

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 com.google.gson.Gson;

public class MainActivity extends Activity {
    private Button tojson;
    RequestQueue mQueue;
    StringRequest stringRequest;
    Gson gson;
    String str;
    private TextView textView;
    private EditText editText;
    String string = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        textView = (TextView) findViewById(R.id.text);
        editText = (EditText) findViewById(R.id.edit);
        tojson = (Button) findViewById(R.id.btn);
        gson = new Gson();
        mQueue = Volley.newRequestQueue(MainActivity.this);
        tojson.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                string = editText.getText().toString();
                String requestUrl = getRequestUrl(string);
                stringRequest = new StringRequest(requestUrl,
                        new Response.Listener<String>() {
                            public void onResponse(String response) {

                                Log.d("TAG", response);
                                System.out.println("response=" + response);
                                Status1 status = gson.fromJson(response,
                                        Status1.class);
                                StringBuffer buffer = new StringBuffer();// 保存所用字符串

                                RetData2 retData2 = status.getRetData();// 第二个对象的获取

                                System.out.println("from=" + retData2.getFrom());
                                DictResult3 dictResult3;
                                dictResult3 = retData2.getDictResult();// 第三个对象的获取
                                buffer.append("单词:"
                                        + dictResult3.getWord_name() + "n");
                                System.out.println("word_name="
                                        + dictResult3.getWord_name());
                                List<Symbols4> symbols4s = dictResult3
                                        .getSymbols();// 第四个是对象数组哦,获取对象数组
                                buffer.append("音标"
                                        + symbols4s.get(0).getPh_en() + "n");// symbols4s.get(0)用于获取第一个对象
                                List<Parts> parts = symbols4s.get(0).getParts();// 同理,最后一个也是对象数组
                                for (int i = 0; i < parts.size(); i++) {
                                    buffer.append("part:"
                                            + parts.get(i).getParts()// parts.get(i)获取对象List中的各个对象
                                            + "n");
                                    buffer.append("词义:");
                                    for (int j = 0; j < parts.get(i).getMeans().length; j++) {
                                        String[] aStrings = parts.get(i)
                                                .getMeans();
                                        buffer.append(aStrings[j]);
                                    }
                                    buffer.append("n");
                                }

                                textView.setText(buffer);

                            }
                        }, new Response.ErrorListener() {
                            public void onErrorResponse(VolleyError error) {
                                Log.e("TAG", error.getMessage(), error);
                            }

                        });
                mQueue.add(stringRequest);
            }
        });

    }

    private String getRequestUrl(String word) {
        String url = null;
        if (word != null) {
            url = "http://apistore.baidu.com/microservice/dictionary?query="
                    + word + "&from=en&to=zh";
        }
        return url;
    }

}

运行实例:

以上实例只是展示如何使用,有很多Bug有待完善,大家可以自行改善,也可以发挥你的能力,做成一部真正的字典app。

源码下载地址:http://download.csdn.net/detail/yayun0516/8716107

我的应用下载地址:

http://openbox.mobilem.360.cn/index/d/sid/2966005

http://android.myapp.com/myapp/detail.htm?apkName=com.yayun.gitlearning

欢迎下载,有问题多交流!(喜欢的请关注我,谢谢!)

标签: android

热门推荐