⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 midpapplication.java

📁 一组http交互程序 ConnectHttpMIDlet HttpCookieMIDlet.java HttpGETMIDlet.java HttpHEADMIDlet.java Ht
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

public class MIDPApplication extends MIDlet 
implements CommandListener {
  Ticker newsTicker;               //头条新闻用的跑马灯
  private List menuList;          //主菜单
  private List luckyList;         //一周运势菜单
  private List commList;          //通讯簿菜单

  private Command exitCommand;   //离开
  private Command execCommand;   //执行
  private Command menuCommand;   //主目录
  private Command commCommand;   //通讯簿
  private Command luckyCommand;  //一周运势
  private Command saveCommand;   //存储

  Form queryForm = null;
  Form modificationForm = null;
  Form deleteForm = null;

  TextField nameField;
  TextField EMailField;
  TextField ageField;

  String[] menuItem ={"通讯簿","一周运势"};
  String[] commItem ={"新增","查询","修改","删除"};
  String[] luckyItem ={"金牛座","处女座","摩羯座",
   	               "射手座","牡羊座","狮子座",
   	               "双鱼座","巨蟹座","天蝎座",
   	               "双子座","天秤座","水瓶座"
 	              };

  Image[] menuImage = {createImage("/Sokoban.png"),
                            createImage("/Tiles.png")
                          };

  private Display display;
  private String currentScreen = "";
  public static String  criteria = "";
  private int deleteID = 0;

  private String newsURL = "http://localhost/JIDCA/fetchNews.asp";
  private String starsURL = "http://localhost/JIDCA/fetchStars.asp";
  private String newPersonURL = "http://localhost/JIDCA/newperson.asp";
  private String queryPersonURL = "http://localhost/JIDCA/queryperson.asp";
  private String deletePersonURL = "http://localhost/JIDCA/delperson.asp";
  private String modificationPersonURL = 
"http://localhost/JIDCA/modiperson2.asp";

  public MIDPApplication() {
    display = Display.getDisplay(this);

    // 从网站下载头条新闻的内容-----------------------------------
    String result="";
	try{
		result = requestUsingPOST(newsURL,"");
	}
	catch(IOException e){
		result = "联机失败!";
	}
    // 将取得的头条新闻设置给Ticker对象
    newsTicker = new Ticker(result);//----------------------------

    exitCommand = new Command("离开", Command.EXIT, 1);
    execCommand = new Command("执行",Command.OK, 1);
    menuCommand = new Command("主目录",Command.SCREEN, 1);
    commCommand = new Command("通讯簿",Command.SCREEN, 1);
    luckyCommand = new Command("运势",Command.SCREEN,1);
  }

  public void startApp() {
    menuList = new List(null,List.IMPLICIT,menuItem,menuImage);
    menuList.setTicker(newsTicker);
menuList.addCommand(exitCommand);
menuList.setCommandListener(this);
display.setCurrent(menuList);
currentScreen="主目录";
  }

  public void pauseApp() { }
  public void destroyApp(boolean unconditional) { }
  public void commandAction(Command c, Displayable s) {
    if (c == exitCommand) {
        destroyApp(false);
        notifyDestroyed();
    }

    if(c==List.SELECT_COMMAND){
	if(currentScreen=="主目录"){
	int itemIndex = menuList.getSelectedIndex();
	switch(itemIndex){
		case 0: {
			doCommunication();
			break;
			}
	case 1: doLucky();
	}
	}
	else if(currentScreen=="通讯簿"){
           int itemIndex = commList.getSelectedIndex();
           switch(itemIndex){
		case 0:{
		doAppend();
		break;
		}
	case 1:{
		doQuery();
		break;
		}
	case 2:{
		doModification();
		break;
		}
		case 3:{
		doDelete();
		break;
		}
	}

	}
	else{

	}
	}

	if(c==menuCommand){
	display.setCurrent(menuList);
            currentScreen="主目录";
	}

	if(c==commCommand){
	doCommunication();
	}

	if(c==luckyCommand){
	doLucky();
	}

    if(c==menuCommand){
       doMenu();
}

if(c==execCommand){
		if(currentScreen=="一周运势"){
			showLucky();
		}
		if(currentScreen=="通讯簿查询"){
			this.criteria = nameField.getString();
			doQueryPerson();
			queryForm = null;
		}
		if(currentScreen=="通讯簿删除"){
            doDeletePerson();
		}
		if(currentScreen=="通讯簿修改"){
			doModificationPerson();
		}
	}
//------存储通讯簿的数据
    if(c==saveCommand)
      doSave();
}

 private Image createImage(String name){
      Image aImage = null;
      try{
         aImage = Image.createImage(name);
      }
      catch(IOException e){
      }
      return aImage;
 }

 private void doMenu(){
     currentScreen="主目录";
     display.setCurrent(menuList);
 }

 private void doCommunication(){
     Image[] commIcon = {createImage("/StarCruiser.png"),
                         createImage("/Stock.png"),
                         createImage("/ManyBalls.png"),
                         createImage("/Tiles.png")
                        };

commList = new List("通讯簿",List.IMPLICIT,commItem,commIcon);

     commList.addCommand(luckyCommand);
     commList.addCommand(menuCommand);
     commList.setCommandListener(this);
     currentScreen="通讯簿";
     display.setCurrent(commList);
 }

  private void doLucky(){
luckyList = new List("一周运势",List.EXCLUSIVE,luckyItem,null);
     luckyList.addCommand(commCommand);
     luckyList.addCommand(menuCommand);
     luckyList.addCommand(execCommand);
     luckyList.setCommandListener(this);
     currentScreen="一周运势";
     display.setCurrent(luckyList);
 }

 private void showLucky(){
	 int selectedItem = luckyList.getSelectedIndex();
	 Alert alert;
	 String information = "";

	 switch(selectedItem){
		 case 0:{
			 information = "information=s1";//不要使用中文
			 break;
		 }
		 case 1:{
			 information = "information=s2";
			 break;
		 }
		 case 2:{
			 information = "information=s3";
			 break;
		 }
		 case 3:{
			 information = "information=s4";
			 break;
		 }
		 case 4:{
			 information = "information=s5";
			 break;
		 }
		 case 5:{
			 information = "information=s6";
			 break;
		 }
		 case 6:{
			 information = "information=s7";
			 break;
		 }
		 case 7:{
			 information = "information=s8";
			 break;
		 }
		 case 8:{
			 information = "information=s9";
			 break;
		 }
		 case 9:{
			 information = "information=s10";
			 break;
		 }
		 case 10:{
			 information = "information=s11";
			 break;
		 }
		 case 11:{
			 information = "information=s12";
			 break;
		 }
	 }

     // 从网站下载被选择的星座其内容--------------------------------------
	 String result="";
	 try{
	 	 result = requestUsingPOST(starsURL,information);
	 }
	 catch(IOException e){
	 	 result = "联机失败!";
	 }

	 // 将取得的星座内容设置给Alert对象
	 alert = new Alert("信息",result,null,AlertType.INFO);//--------------
	 alert.setTimeout(Alert.FOREVER);
	 display.setCurrent(alert);
 }

 private void doAppend(){
     Form appendForm = new Form("新增");
nameField = new TextField("姓名",null,8,TextField.ANY);
EMailField = new TextField("E Mail",null,30,TextField.EMAILADDR);
ageField = new TextField("年龄",null,3,TextField.NUMERIC);

saveCommand = new Command("存储",Command.SCREEN,1);
appendForm.append(nameField);
appendForm.append(EMailField);
appendForm.append(ageField);
appendForm.addCommand(saveCommand);
appendForm.addCommand(commCommand);
appendForm.addCommand(luckyCommand);
appendForm.addCommand(menuCommand);
     appendForm.setCommandListener(this);
currentScreen = "通讯簿新增";
display.setCurrent(appendForm);
 }
 private void doModification(){
     modificationForm = new Form("修改");
nameField = new TextField("姓名",null,10,TextField.ANY);
     EMailField = new TextField("E Mail",null,30,TextField.EMAILADDR);
     ageField = new TextField("年龄",null,3,TextField.NUMERIC);

     modificationForm.append(nameField);
     modificationForm.append(EMailField);
     modificationForm.append(ageField);
     modificationForm.addCommand(execCommand);
     modificationForm.addCommand(commCommand);
     modificationForm.addCommand(luckyCommand);
     modificationForm.addCommand(menuCommand);
     modificationForm.setCommandListener(this);
     currentScreen = "通讯簿修改";
     display.setCurrent(modificationForm);
 }
 private void doQuery(){
     queryForm = new Form("查询");
     nameField = new TextField("姓名",null,10,TextField.ANY);
     queryForm.append(nameField);
     queryForm.addCommand(execCommand);
     queryForm.addCommand(commCommand);
     queryForm.addCommand(luckyCommand);
     queryForm.addCommand(menuCommand);
     queryForm.setCommandListener(this);
     currentScreen = "通讯簿查询";
     display.setCurrent(queryForm);
 }
 private void doDelete(){
     deleteForm = new Form("删除");
     nameField = new TextField("姓名",null,10,TextField.ANY);
     deleteForm.append(nameField);
     deleteForm.addCommand(execCommand);
     deleteForm.addCommand(commCommand);
     deleteForm.addCommand(luckyCommand);
     deleteForm.addCommand(menuCommand);
     deleteForm.setCommandListener(this);
     currentScreen = "通讯簿删除";
     System.out.println("delete proc1");
     display.setCurrent(deleteForm);
 }

 private void doSave(){
	 //将通讯簿数据传送至网站--------------------------------------
     String result="";
     String newRecord = "txtName=" + nameField.getString() + "&" +
                        "txtEMail=" + EMailField.getString() + "&" +
                        "txtAge=" + ageField.getString() ;

     try{
	 	 result = requestUsingPOST(newPersonURL,newRecord);
 }
     catch(IOException e){
	  	 result = "联机失败!";
	 }

	 // 将存储结果设置给Alert对象
     Alert alert = new Alert("信息",result,null,AlertType.INFO);//--------------
     alert.setTimeout(Alert.FOREVER);
     display.setCurrent(alert);
 }

  private void doQueryPerson(){
     // 从网站下载查询的通讯簿内容--------------------------------------
     String result="";
     String queryString = "txtName=" + this.criteria;

     System.out.println("queryString:"+queryString);
     try{
	 	 result = requestUsingPOST(queryPersonURL,queryString);
	 }
     catch(IOException e){
	  	 result = "联机失败!";
	 }

     // 将取得的通讯簿内容设置给Alert对象
     Alert alert = new Alert("信息",result,null,AlertType.INFO);//--------------
     alert.setTimeout(Alert.FOREVER);
     display.setCurrent(alert);
 }

   public void doModificationPerson(){
     // 将要修改的记录传送给的网站里的通讯簿------------------------------------
     String result="";
     String modificationRecord = "txtName=" + nameField.getString() + "&" +
                                 "txtEMail=" + EMailField.getString() + "&" +
                                 "txtAge=" + ageField.getString() ;

     try{
 	result = requestUsingPOST(modificationPersonURL,modificationRecord );
 	 }
     catch(IOException e){
 	  	 result = "联机失败!";
 	 }

       // 将修改的结果设置给Alert对象
     Alert alert = new Alert("信息",result,null,AlertType.INFO);//--------------
      alert.setTimeout(Alert.FOREVER);
      display.setCurrent(alert);
    }

   public void doDeletePerson(){
     // 从网站删除符合用户输入名称相同的通讯簿记录内容------------------------
     String result="";
     String deleteString = "txtName=" + nameField.getString();

     try{
	 	 result = requestUsingPOST(deletePersonURL,deleteString);
	 }
     catch(IOException e){
	  	 result = "联机失败!";
	 }

     // 将删除的结果设置给Alert对象
     Alert alert = new Alert("信息",result,null,AlertType.INFO);//--------------
     alert.setTimeout(Alert.FOREVER);
     display.setCurrent(alert);
   }

   private String requestUsingPOST(String URL,String URLString) 
throws IOException{
     HttpConnection hpc = null;
     DataInputStream dis = null;
      DataOutputStream dos = null;
     InputStreamReader xdis = null;

      boolean newline = false;
     String content = "";
      System.out.println(URL + ":" + URLString);

     try{
  	hpc = (HttpConnection)Connector.open(URL);

  	hpc.setRequestMethod(HttpConnection.POST);
  	hpc.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
  	hpc.setRequestProperty("Content-Language","zh-tw");
  	hpc.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
  	hpc.setRequestProperty("Content-Length",
String.valueOf(URLString.length()));

  	dos = new DataOutputStream(hpc.openOutputStream());
  	dos.write(URLString.getBytes());
  	dos.flush();

  	int status = hpc.getResponseCode();
  		  if(status != HttpConnection.HTTP_OK){
  		    content = "找不到网页 !";
  		    return content;
	      }

    	xdis = new InputStreamReader(hpc.openInputStream());
          int character;
  	while((character = xdis.read()) != -1 ){
  			  if((char)character =='\\'){
  				newline = true;
  			    continue;
  			  }
  			  else {
  				    if((char)character =='n' && newline){
  			           content += "\n";
  			           newline = false;
  		             }
  		             else if(newline){
  		                content += "\\" +(char)character ;
  		                newline = false;
  					}
     			    else {
  			            content += (char)character;
  			            newline = false;
  				    }
  			  }
   		  }
  	  }
  	  catch(IOException e){
  		  System.out.println("error:"+e);
  	  }

  	  finally{
		  System.out.println("finally");
  		  if(hpc != null){
			  System.out.println("closeed");
  		     hpc.close();
  		     hpc=null;}
  		  if(xdis != null)
  		     xdis.close();
  	  }
  	  return content;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -