connectasp.java.bak

来自「书中的例题」· BAK 代码 · 共 86 行

BAK
86
字号
/***************************************************
*  程序文件名称: ConnectASP..java
*  功能:实现对网络服务器的访问。
***************************************************/
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class ConnectASP extends MIDlet{
    private  Display  myDisplay; 
    private  Alert    alert ;
   public  ConnectASP()
	{
      myDisplay = Display.getDisplay(this); 
	}
   
   public void startApp(){
     try{
		 alert= new Alert("接收网络信息");
	     alert.setTimeout(Alert.FOREVER);
		 String URLString="http://192.168.16.6/ConnectASP.asp";
         String str=requestGET(URLString,"");
		 alert.setString(str);    
		 myDisplay.setCurrent(alert); 
       }catch(IOException e){System.out.println("error");
     }
}

public String requestGET(String URLString, String str) throws IOException
 {
   // =============================================================
   // URLString是HTTP地址,str为后面的参数
   // 这里的例子是发送用的用户名和密码到服务器端进行用户验证
   // 比如 String URLString = "http://192.168.0.1:8080/login.asp";
   // String str = "?Name="+this.Name+"&Pass="+this.Pass
   // =============================================================
 HttpConnection hpc = null;
 DataInputStream dis = null;
 boolean newline = false;
 String content = "";
 try{
       // ============================================= 
       // 建立连接
       // ============================================= 
      hpc = (HttpConnection)Connector.open(URLString+str);
      hpc.setRequestMethod(HttpConnection.GET);
      dis = new DataInputStream(hpc.openInputStream());
      int character;
      // ============================================== 
      // 读取返回的HTTP内容
      // ============================================== 
      while((character = dis.read()) != -1){
            content +=(char)character;
            newline = false;
      }
    }
    catch(IOException e){System.out.print("ERROR:"+e);}
    finally{
      if(hpc != null){
           hpc.close();
           hpc = null;
      }
      if(dis != null){
           dis.close();
      }
    }
    // ===============================================================
    // 由于内容可能有中文,所以在接受到信息后要对内容进行字符集的转换
    // ===============================================================
    content = (unicodeTogb2312(content)).trim();
    return content;
   }
 
   public static String unicodeTogb2312(String s){
    if (s==null){ return ""; }
    if (s.equals("")){ return s;  }
    try{
        return new String(s.getBytes("ISO8859_1"),"gb2312");
    }
    catch(Exception uee){ return s; }
  }
 
  public void pauseApp(){}
  public void destroyApp(boolean unconditional){}
}

⌨️ 快捷键说明

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