Chris Pollett > Students >
Rekha

    ( Print View )

    [Bio]

    [CS297Proposal]

    [Del1]

    [Del2]

    [Del3]

    [Del4]

    [CS297 Report-PDF]

    [CS298 Proposal]

    [CS298 Presentation-PDF]

    [Game Description-PDF]

    [Final Game-ZIP]

    [CS298 Report-PDF]

                          

























Deliverable 3

Description: This deliverable shows how to make a connection from PDA to the HTTP Server. The user of the PDA can invoke the application on the PDA which takes in the details of the baby. These details are put in the XML format and then the user can click the Submit button to connect to the Tomcat server. This deliverable is extended with the deliverable four using which the user can upload the baby details to the Oracle Database.

Example:This is the Welcome screen for the application. A menu is presented to the user. Using this menu, the user can enter the baby details by selecting the first option "Add Baby Details" and can quit using the second option.

Welcome Screen of the application

Example:This is the screen in which the user can enter the information about the baby. After entering the information, the user can submit the information

GUI screen

Example:This is the screen which shows that the XML document which contains the baby details has been uploaded.

Success screen

Using this application, the user can enter the baby details to the HTTP Server.

/**

   The Pocket PC Application

   Solves Deliverable 3

   @author Rekha Vaddepalli

   @version 1.00 2003/11/15

*/

import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.util.Enumeration;
import java.util.Vector;
import java.io.*;

/**
  This class is the application to be installed on the Pocket PC
*/
public class BabyHTTP extends MIDlet implements CommandListener
{

    private static String url =
    "http://anouki:8088/examples/servlet/XMLOracleservlet";
    private Display display = null;
       // Declaring variables for Display class.
    Command quit = null;
       // Declaring variables for Buttons in the UI.
    Command addnow = null;
       // Declaring variables for Buttons in the UI.
    Command mainmenu = null;
       // Declaring variables for Buttons in the UI.
    Command okCommand   = new Command( "OK", Command.OK, 1 );
    List menu = null;    // Declaring a Menu(List)
    Form ui_form = null;

    /* TextFields for the User Interface */
    TextField babyname = null;
    TextField momname = null;
    TextField dadname = null;
    TextField docname = null;
    TextField duedate = null;
    TextField hospitalname = null;
    TextField birthdate = null;
    TextField birthtime = null;
    TextField weight = null;
    TextField height = null;
    TextField coloreyes = null;
    TextField colorhair = null;
    TextField birthstone = null;
    TextField flower = null;
    TextField nicknames = null;
    TextField pediatricianname = null;
    TextField firstbdvenue = null;
    TextField firstbdcake = null;
    TextField firstbdoutfit = null;

    /* Declaring the Record Store */
    RecordStore recordStore = null;

    /*
     Constructor of the class
     Initializes the  components
     @author Rekha vaddepalli
    */
    public BabyHTTP()
    {
     /* Initializing the Display,Command Buttons, Record Store */
     display = Display.getDisplay(this);
	 quit = new Command("Quit",Command.SCREEN,3);
	 addnow = new Command("Add",Command.SCREEN,2);
	 mainmenu = new Command("Main",Command.SCREEN,2);
	 try
	 {
        recordStore = RecordStore.openRecordStore("babyDetails", true);
     }
     catch(RecordStoreException rse)
     {
        rse.printStackTrace();
     }
    }

	/**
	 The Midlet StartApp method. It shows the screen
	 to be shown when the application
       starts.
       @author Rekha Vaddepalli
	*/
	public void startApp()
	{
        menu = new List("Baby Details...",List.IMPLICIT);
	  menu.append("1. Add Baby Details",null);
        menu.append("2. Quit",null);
	  menu.setCommandListener(this);
	  display.setCurrent(menu);
	}

      /**
       GUI method for the Adding Entry screen
       @author Rekha Vaddepalli
      */
	void addScreen()
	{
		ui_form = new Form("Add an Entry..");

		babyname =  new TextField("Baby Name ..","",50,0);
		ui_form.append(babyname);
		momname =  new TextField("Mom's Name.. ","",50,0);
		ui_form.append(momname);
		dadname =  new TextField("Dad's Name.. ","",50,0);
		ui_form.append(dadname);
		docname =  new TextField("Doc's Name.. ","",50,0);
		ui_form.append(docname);
		duedate =  new TextField("Due Date.. ","",50,0);
		ui_form.append(duedate);
		hospitalname =  new TextField("Hospital Name.. ","",50,0);
		ui_form.append(hospitalname);
		birthdate =  new TextField("Birth Date.. ","",50,0);
		ui_form.append(birthdate);
		birthtime =  new TextField("Birth Time.. ","",50,0);
		ui_form.append(birthtime);
		weight =  new TextField("Weight.. ","",50,0);
		ui_form.append(weight);
		height =  new TextField("Height.. ","",50,0);
		ui_form.append(height);
		coloreyes =  new TextField("Color of Eyes.. ","",50,0);
		ui_form.append(coloreyes);
		colorhair =  new TextField("Color of Hair.. ","",50,0);
		ui_form.append(colorhair);
		birthstone =  new TextField("BirthStone.. ","",50,0);
		ui_form.append(birthstone);
		flower =  new TextField("Flower.. ","",50,0);
		ui_form.append(flower);
		nicknames =  new TextField("NickNames.. ","",50,0);
		ui_form.append(nicknames);
		pediatricianname =  new TextField("Pediatrician Name.. ","",50,0);
		ui_form.append(pediatricianname);
		firstbdvenue =  new TextField("First Birthday Venue.. ","",50,0);
		ui_form.append(firstbdvenue);
		firstbdcake =  new TextField("First Birthday Cake.. ","",50,0);
		ui_form.append(firstbdcake);
		firstbdoutfit =  new TextField("First Birthday Outfit.. ","",50,0);
		ui_form.append(firstbdoutfit);

		ui_form.addCommand(addnow);
		    // Adding Command Button to the ui_form.
		ui_form.addCommand(quit);
		    // Adding Command Button to the ui_form.
		ui_form.setCommandListener(this);
		    // Invoking Action Listener..
		display.setCurrent(ui_form);
	}


	/**
       Method to pause the application
       @author Rekha Vaddepalli
      */
	public void pauseApp()
	{
         menu = null;
	}

	/**
	 Method to destroy the application.
       @author Rekha Vaddepalli
      */
	public void destroyApp(boolean flag)
	{
	    menu = null;
	    notifyDestroyed();
	}

      /**
       Method to tell the application what to do when a
       particular command button is clicked
       @author Rekha Vaddepalli
      */
	public void commandAction(Command c, Displayable d)
	{
        if ( c == quit )
        {
		try
		{
	        close();
		} catch (RecordStoreException rse)
		{
		  rse.printStackTrace();
		}
		destroyApp(true);
	    } else if( c == addnow )
	    {
		 String temp_babyname = babyname.getString();
		 String temp_momname = momname.getString();
		 String temp_dadname = dadname.getString();
		 String temp_docname = docname.getString();
		 String temp_duedate = duedate.getString();
		 String temp_hospitalname = hospitalname.getString();
		 String temp_birthdate = birthdate.getString();
		 String temp_birthtime = birthtime.getString();
		 String temp_weight = weight.getString();
		 String temp_height = height.getString();
		 String temp_coloreyes = coloreyes.getString();
		 String temp_colorhair = colorhair.getString();
		 String temp_birthstone = birthstone.getString();
		 String temp_flower = flower.getString();
		 String temp_nicknames = nicknames.getString();
		 String temp_pediatricianname = pediatricianname.getString();
		 String temp_firstbdvenue = firstbdvenue.getString();
		 String temp_firstbdcake = firstbdcake.getString();
		 String temp_firstbdoutfit = firstbdoutfit.getString();
             String info[] = new String[19];
		 info[0] = temp_babyname;
		 info[1] = temp_momname;
		 info[2] = temp_dadname;
		 info[3] = temp_docname ;
		 info[4] = temp_duedate ;
		 info[5] = temp_hospitalname;
		 info[6] = temp_birthdate ;
		 info[7] = temp_birthtime ;
		 info[8] = temp_weight ;
		 info[9] = temp_height ;
		 info[10] = temp_coloreyes;
		 info[11] = temp_colorhair;
		 info[12] = temp_birthstone;
		 info[13] = temp_flower ;
		 info[14] = temp_nicknames ;
		 info[15] = temp_pediatricianname;
		 info[16] = temp_firstbdvenue;
		 info[17] = temp_firstbdcake;
		 info[18] = temp_firstbdoutfit;

		baby_add(info);
	   } else if (c == mainmenu)
	   {		// To return to Main Menu...
		startApp();
	   } else if( c == okCommand )
	   {
            startApp();
       } else
       {
		List down = (List)display.getCurrent();
		switch(down.getSelectedIndex()){
		  case 0: addScreen();break;
		  case 1: destroyApp(true);break;
		}
	   }
	}

	/**
        Function for the Entry addition. It forms the entry
        into an XML form and connects
        to the HTTP Server and invokes the HTTP POST operation.
        @author Rekha Vaddepalli
      */
      void baby_add(String[] info)	 // Function for address addtion...
	{
	   String data = "<BabyName>"+info[0]+"</BabyName><
	   				MomName>"+info[1]+
  "</MomName><DadName>"+info[2]+"</DadName><DocName>
  "+info[3]+
  "</DocName><DueDate>"+info[4]+"</DueDate><HospitalName>
  "+info[5]+
  "</HospitalName><BirthDate>"+info[6]+"</BirthDate><BirthTime>"
  +info[7]+
  "</BirthTime><Weight>"+info[8]+"</Weight><Height>"+info[9]+
  "</Height><ColorEyes>"+info[10]+"</ColorEyes><ColorHair>"
  +info[11]+
  "</ColorHair><BirthStone>"+info[12]+"</BirthStone><Flower>"
  +info[13]+
  "</Flower><NickNames>"+info[14]+"</NickNames><
  PediatricianName>"
  +info[15]+
  "</PediatricianName><FirstBirthDayVenue>"+info[16]+
  "</FirstBirthDayVenue><FirstBirthdayCake>"+info[17]+
  "</FirstBirthdayCake><FirstBirthdayOutfit>"+info[18]
  +"</FirstBirthdayOutfit>";

	   ByteArrayOutputStream baos = new ByteArrayOutputStream();
	   DataOutputStream outputStream = new DataOutputStream(baos);
	   try
	   {
	      outputStream.writeUTF(data);
		byte[] b = baos.toByteArray();
		recordStore.addRecord(b,0, b.length);
		System.out.println("Success....");
            System.out.println("One record entered successfully..");
         } catch (IOException ioe)
         {
	      ioe.printStackTrace();
	   } catch (RecordStoreException rse)
	   {
	       rse.printStackTrace();
	   }

	   StatusForm f = new StatusForm(data);
         display.setCurrent( f );
         f.start();
	}

      /** Methos to close the Record Store
        @author Rekha Vaddepalli
      */
	public void close() throws RecordStoreNotOpenException,RecordStoreException
	  {
        recordStore.closeRecordStore();
      }

      /**
        Class to do the HTTP Post and show the resulting status
        @author Rekha Vaddepalli
      */
	class StatusForm extends Form implements Runnable,
	    HttpConnectionHelper.Callback
   {
	    byte[] data1;
	    StringItem message;

	    /**Constructor*/
	    StatusForm( String text ){
            super( "Status" );

            /** Convert the string into a byte array.  */
            try
            {
               ByteArrayOutputStream bout = new ByteArrayOutputStream();
               DataOutputStream dout = new DataOutputStream( bout );
               dout.writeUTF( text );
               data1 = bout.toByteArray();
               dout.close();
            }catch( IOException e )
            {
               System.out.println("IOException in StatusForm"+e.toString());
            )
          }

        /** Method to update the display*/

        void display( String text )
        {
            if( message == null )
            {
                message = new StringItem( null, text );
                append( message );
            } else
            {
                message.setText( text );
            }
        }

        /**Method to show it is done*/
        void done( String msg )
        {
            display( msg != null ? msg : "Done." );
            addCommand( okCommand );
            setCommandListener( BabyHTTP.this );
        }

       /** Callback for making the HTTP connection.*/

        public void prepareRequest(String originalURL, HttpConnection conn )
              throws IOException
              {
            conn.setRequestMethod(HttpConnection.POST );
            conn.setRequestProperty("User-Agent",
            "Profile/MIDP-1.0 Configuration/CLDC-1.0" );
            conn.setRequestProperty("Content-Language", "en-US" );
            conn.setRequestProperty("Accept", "application/octet-stream" );
            conn.setRequestProperty("Connection", "close" );
            conn.setRequestProperty("Content-Length",
            Integer.toString( data1.length ) );
            OutputStream os = conn.openOutputStream();
            os.write( data1 );
            os.close();
        }

       /** To Do the connection on a separate thread to keep the UI responsive*/

       public void run()
       {
            HttpConnection conn = null;
            display( "Obtaining HttpConnection object..." );
            try
            {
                conn = HttpConnectionHelper.connect( url, this );
                display( "Connecting to the server..." );
                int rc = conn.getResponseCode();
                if( rc == HttpConnection.HTTP_OK ){
			  ui_form = new Form("Success.....");
                    ui_form.append("One Record Entered and uploaded Successfully...");
			  ui_form.append("Return Code"+rc);
			  ui_form.addCommand(mainmenu);
			  ui_form.addCommand(quit);
		        ui_form.setCommandListener(BabyHTTP.this);
	              display.setCurrent(ui_form);
                } else
                {
                    done( "Unexpected return code: " + rc );
                }
            }
            catch( IOException e )
            {
                done( "Exception " + e + " trying to connect." );
            }
        }

        /** Starts the upload in the background*/

        void start()
        {
            display( "Starting..." );

            Thread t = new Thread( this );
            try
            {
                t.start();
            }
            catch( Exception e ){
                done( "Exception " + e + " trying to start thread." );
            }
        }
    }
}

/** Using the following class we can automatically follow web site redirections*/
class HttpConnectionHelper
{

    public interface Callback
    {
      void prepareRequest( String originalURL,
                           HttpConnection conn )
           throws IOException;
    }

    public static HttpConnection connect( String url )
                                   throws IOException {
      return connect( url, null );
    }

    public static HttpConnection connect(String url, Callback callback )
                         throws IOException
    {

      String originalURL = url;
      HttpConnection conn = null;
      while( url != null )
      {
         conn =  (HttpConnection)Connector.open( url );
         if( callback != null )
         {
          callback.prepareRequest( originalURL, conn );
         }
         int rc = conn.getResponseCode();
         switch( rc )
         {
            case HttpConnection.HTTP_MOVED_PERM:
            case HttpConnection.HTTP_MOVED_TEMP:
            case HttpConnection.HTTP_SEE_OTHER:
            case HttpConnection.HTTP_TEMP_REDIRECT:
              url = conn.getHeaderField( "Location" );
              if( url != null && url.startsWith("/*" ) )
              {
                StringBuffer b = new StringBuffer();
                b.append( "http://" );
                b.append( conn.getHost() );
                b.append( ':' );
                b.append( conn.getPort() );
                b.append( url );
                url = b.toString();
              }
              conn.close();
              break;
            default:
              url = null;
              break;
        }
      }

      return conn;
   }
 }