Page 311 -
P. 311

mobile app development


                           Android Code Magnets


                           Here is the code to a program that queries your web server for the list of names as a JSON array
                           and then displays the list on the smartphone. The only trouble is, the second half of the program
                           is a bunch of mixed-up code magnets at the bottom of the screen. Your job is to rearrange the
                           magnets to complete the program.
                           import android
                           import json
                           import time                     Do the usual imports…these
                                                           ones pull in web client
                                                           functionality.
                           from urllib import urlencode
            All of this    from urllib2 import urlopen
            program’s
            messages are in
            one place.      hello_msg     = "Welcome to Coach Kelly's Timing App"
                           list_title    = 'Here is your list of athletes:'
                            quit_msg      = "Quitting Coach Kelly’s App."
            The name        web_server    = 'http://192.168.1.33:8080'         Change this to the
                                                                               web address that’s
            of the CGI      get_names_cgi = '/cgi-bin/generate_names.py'       running your web
            script to run
            on the web      def send_to_server(url, post_data=None):            server.
            server
                                if post_data:
                                                                                This function takes both a
                                    page = urlopen(url, urlencode(post_data))   web address (url) and some
                                else:                                           optional data (post_data)
                                    page = urlopen(url)                         and sends a web request to
                                return(page.read().decode("utf8"))              your web server. The web
                                                                                response is returned to the
                                                                                caller.
          This code’s a
          mess…can you
          fix it?




           athlete_names = sorted(json.loads(send_to_server(web_server + get_names_cgi)))


          status_update(quit_msg)                               resp = app.dialogGetResponse().result
            app.dialogShow()         app.dialogCreateAlert(list_title)
                                                                             status_update(hello_msg)
                   def status_update(msg, how_long=2):
                       app.makeToast(msg)                    app.dialogSetPositiveButtonText('Select')
                        time.sleep(how_long)                  app = android.Android()

           app.dialogSetNegativeButtonText('Quit')
                                                             app.dialogSetSingleChoiceItems(athlete_names)
                                                                                      you are here 4    275
   306   307   308   309   310   311   312   313   314   315   316