Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android實戰--英文詞典(API+GSON+Volley)

Android實戰--英文詞典(API+GSON+Volley)

編輯:關於Android編程

轉載請注明出處,喜歡我的可以關注我!

上一節我們介紹了GSON和Volley,用GSON對返回的數據進行了初步解析,這一節我們更進一步,討論一下如何實現英文詞典。首先把JSON數據的樣式再看一下:

 

data-cke-saved-src=https://www.android5.online/Android/UploadFiles_5356/201702/2017022314224548.png

下面創建bean,這些都和上一節一樣:

 

第一個bean:

[java] view plaincopy
  1. package org.lxh.demo;
  2.  
  3. import java.util.List;
  4.  
  5. public class Status1 {
  6. private int errNum;
  7. private String errMsg;
  8. RetData2 retData;//保證變量名與返回JSON的標簽名一致,注意!!!!!!!!
  9. @Override
  10. public String toString() {
  11. return Status [errNum= + errNum + , errMsg= + errMsg + , retData=
  12. + retData + ];
  13. }
  14. public int getErrNum() {
  15. return errNum;
  16. }
  17. public void setErrNum(int errNum) {
  18. this.errNum = errNum;
  19. }
  20. public String getErrMsg() {
  21. return errMsg;
  22. }
  23. public void setErrMsg(String errMsg) {
  24. this.errMsg = errMsg;
  25. }
  26. public RetData2 getRetData() {
  27. return retData;
  28. }
  29. public void setRetData(RetData2 retData) {
  30. this.retData = retData;
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. }

    第二個bean:

    [java] view plaincopy
    1. package org.lxh.demo;
    2.  
    3. import java.util.List;
    4.  
    5. public class RetData2 {
    6. private String from;
    7. private String to;
    8. DictResult3 dict_result;
    9. public String getFrom() {
    10. return from;
    11. }
    12. public void setFrom(String from) {
    13. this.from = from;
    14. }
    15. public String getTo() {
    16. return to;
    17. }
    18. public void setTo(String to) {
    19. this.to = to;
    20. }
    21. public DictResult3 getDictResult() {
    22. return dict_result;
    23. }
    24. public void setDictResult(DictResult3 dictResult) {
    25. this.dict_result = dictResult;
    26. }
    27. @Override
    28. public String toString() {
    29. return RetData [from= + from + , to= + to + , dictResult=
    30. + dict_result + ];
    31. }
    32.  
    33.  
    34.  
    35. }


      第3個bean:

      [java] view plaincopy
      1. package org.lxh.demo;
      2.  
      3. import java.util.List;
      4.  
      5. public class DictResult3 {
      6. private String word_name;
      7.  
      8. private List symbols;
      9.  
      10. public String getWord_name() {
      11. return word_name;
      12. }
      13.  
      14. public void setWord_name(String word_name) {
      15. this.word_name = word_name;
      16. }
      17.  
      18. public List getSymbols() {
      19. return symbols;
      20. }
      21.  
      22. public void setSymbols(List symbols) {
      23. this.symbols = symbols;
      24. }
      25.  
      26. @Override
      27. public String toString() {
      28. return DictResult [word_name= + word_name + , symbols= + symbols
      29. + ];
      30. }
      31.  
      32. }


        第4個bean:

         

        [java] view plaincopy
        1. package org.lxh.demo;
        2.  
        3. import java.util.List;
        4.  
        5. public class Symbols {
        6. private String ph_am;
        7. private String ph_en;
        8. private List parts;
        9.  
        10. public String getPh_am() {
        11. return ph_am;
        12. }
        13. public void setPh_am(String ph_am) {
        14. this.ph_am = ph_am;
        15. }
        16. public String getPh_en() {
        17. return ph_en;
        18. }
        19. public void setPh_en(String ph_en) {
        20. this.ph_en = ph_en;
        21. }
        22. public List getParts() {
        23. return parts;
        24. }
        25. public void setParts(List parts) {
        26. this.parts = parts;
        27. }
        28. @Override
        29. public String toString() {
        30. return Symbols [ph_am= + ph_am + , ph_en= + ph_en + , parts=
        31. + parts + ];
        32. }
        33.  
        34.  
        35. }


          第5個:

          [java] view plaincopy
          1. package org.lxh.demo;
          2.  
          3. import java.util.Arrays;
          4. import java.util.List;
          5.  
          6. public class Parts {
          7. private String part;
          8. public String getParts() {
          9. return part;
          10. }
          11. public void setParts(String parts) {
          12. this.part = parts;
          13. }
          14. public String[] getMeans() {
          15. return means;
          16. }
          17. public void setMeans(String[] means) {
          18. this.means = means;
          19. }
          20. @Override
          21. public String toString() {
          22. return Parts [parts= + part + , means= + Arrays.toString(means)
          23. + ];
          24. }
          25. String[] means;
          26.  
          27.  
          28.  
          29.  
          30. }


            所需要的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() {
            							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() + 
            );
            								System.out.println(word_name=
            										+ dictResult3.getWord_name());
            								List symbols4s = dictResult3
            										.getSymbols();// 第四個是對象數組哦,獲取對象數組
            								buffer.append(音標
            										+ symbols4s.get(0).getPh_en() + 
            );// symbols4s.get(0)用於獲取第一個對象
            								List parts = symbols4s.get(0).getParts();// 同理,最後一個也是對象數組
            								for (int i = 0; i < parts.size(); i++) {
            									buffer.append(part:
            											+ parts.get(i).getParts()// parts.get(i)獲取對象List中的各個對象
            											+ 
            );
            									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(
            );
            								}
            
            								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。

             

             

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved