Page 317 -
P. 317

mobile app development


          The athlete’s data CGI script


           Here’s the code for the cgi-bin/generate_data.py CGI script, which
           takes a web request and returns the indicated athlete’s data from the model:


                                 #! /usr/local/bin/python3


                                 import cgi
                                 import json
                                 import athletemodel                 Get all the data
                                 import yate                        from the model.
              Process the
              data sent with     athletes = athletemodel.get_from_store()
              the request and
              extract the        form_data = cgi.FieldStorage()                           Start a web
              athlete’s name.    athlete_name = form_data['which_athlete'].value          response, with JSON
                                 print(yate.start_response('application/json'))           as the data type.
                                 print(json.dumps(athletes[athlete_name]))
                                                                                 Include the indicated

                                                                                 response, formatted by JSON.
          The complete Android app, so far                                       athlete’s data in the web
          You’ve made quite a few changes to your program at this stage. Before you test it
           on the Android emulator, take a moment to look at your code in its entirety:


                   import android
                   import json
                   import time

                   from urllib import urlencode
                   from urllib2 import urlopen

                   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 rest of your
                   web_server    = 'http://192.168.1.34:8080'
                                                                                           code is on the
                   get_names_cgi = '/cgi-bin/generate_names.py'                            following page.
                   get_data_cgi  = '/cgi-bin/generate_data.py'


                                                                                      you are here 4    281
   312   313   314   315   316   317   318   319   320   321   322