Android practice-English Dictionary (API + GSON + Volley)

Source: Internet
Author: User
Tags tojson

Android practice-English Dictionary (API + GSON + Volley)

Please indicate the source for reprinting. If you like me, follow me!

In the previous section, we introduced GSON and Volley, and used GSON to analyze the returned data. This section further describes how to implement an English dictionary. First, let's take a look at the JSON data style:

 

The bean created below is the same as in the previous section:

 

The first 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; // ensure that the variable name is consistent with the tag name returned in JSON. Note !!!!!!!!
  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. }

    The second 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;
    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;
    17. }
    18. Public void setTo (String ){
    19. This. 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. }


      3rd beans:

      [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. }


        4th beans:

         

        [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. }


          5th:

          [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. }


            The required jar package is the same as that in the previous section. For more information, see the previous section. We will mainly discuss the MainActivity. java program:

            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; @ Overrideprotected 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 (); // Save the string RetData2 retData2 = status. getRetData (); // obtain the second object System. out. println (from = + retData2.getFrom (); DictResult3 dictResult3; dictResult3 = retData2.getDictResult (); // obtain the buffer for the third object. append (word: + dictResult3.getWord _ name () +); System. out. println (word_name = + dictResult3.getWord _ name (); List
                        
                         
            Symbols4s = dictResult3.getSymbols (); // The fourth is the object array. Obtain the object Array buffer. append (phonetic symbol + symbols4s. get (0 ). getPh_en () +); // symbols4s. get (0) is used to obtain the first object List.
                         
                          
            Parts = symbols4s. get (0 ). getParts (); // Similarly, the last one is the object array for (int I = 0; I <parts. size (); I ++) {buffer. append (part: + parts. get (I ). getParts () // parts. get (I) get each object in the Object List +); buffer. append (meaning :); 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 ;}}
                         
                        
                       

             

            Running instance:

             

            The above example only shows how to use it. There are many bugs to be improved. You can improve it on your own, or make full use of your capabilities to make a real dictionary app.

             

             

Related Article

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.