/* * HttpPostDemo.java * * Created on 13 ??????? 2546, 16:38 ?. */
      
        import java.io.*;
        import javax.microedition.io.*;
        import javax.microedition.midlet.*;
        import javax.microedition.lcdui.*;
      /**  
        * @author sup98
        * @version
        */
        public class HttpPostDemo extends MIDlet implements CommandListener{

	  private Display display;
	  private Command exitCommand;
	  private Alert alert = new Alert("Your choice", "", null, null);
	  String url = "http://localhost/HttpPostDemo.php";
	  private List l1;
	  private String choice[] ={"Java", "Pascal" ,"C++" ,"Fortran"};

        
        public HttpPostDemo() {
		display = Display.getDisplay(this);
		exitCommand = new Command("Exit", Command.SCREEN, 1);
        }
        
        /**
        * Start up the Hello MIDlet by creating the TextBox and associating
        * the exit command and listener.
        */
        public void startApp() {
		l1 = new List("Language", Choice.IMPLICIT);
		for(int i=0; i<choice.length; i++){
			l1.append(choice[i],null);
		}
		l1.addCommand(exitCommand);
		l1.setCommandListener(this);
		display.setCurrent(l1);
        }
        
        /**
        * Pause is a no-op since there are no background activities or
        * record stores that need to be closed.
        */
        public void pauseApp() {
        }
        
        /**
        * Destroy must cleanup everything not handled by the garbage collector.
        * In this case there is nothing to cleanup.
        */
        public void destroyApp(boolean unconditional) {
        }
	  
	  void invokeASP(String url, String lang) throws IOException{
		HttpConnection c = null;
		OutputStream out = null;
		InputStream is = null;
		StringBuffer b = new StringBuffer();
		TextBox t = null;
		try{
			c = (HttpConnection)Connector.open(url);
			c.setRequestMethod(HttpConnection.POST);
			c.setRequestProperty("IF-Modified-Since", "20 Jan 2001 16:18:14 GMT");
			c.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
			c.setRequestProperty("Content-Language", "en-US");
			c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			String postString = "lang="+lang;
			c.setRequestProperty("Content-Length", String.valueOf(postString.length()));
			out = c.openOutputStream();
			out.write(postString.getBytes());
			out.flush();
		
			is = c.openDataInputStream();
			int ch;
			while((ch = is.read()) != -1){
				b.append((char) ch);				
			}
			alert.setString(b.toString());

		}finally{
			if(is!=null){
				is.close();
			}
			if(c!=null){
				c.close();
			}
		}
		display.setCurrent(alert, l1);
	  }		

	  public void commandAction(Command c, Displayable s){
		if(c==exitCommand){
			destroyApp(false);
			notifyDestroyed();
		}else if(c==List.SELECT_COMMAND){
			try{
				invokeASP(url, choice[((List)s).getSelectedIndex()]);
			}catch(IOException e){
				System.out.println("IOException " + e);
				e.printStackTrace();
			}
		}
	  }       
        }
