📄 connector.java
字号:
/**
*
*/
package connectpacket;
/**
* @author Administrator
*
*/
import javax.microedition.io.*;
import java.io.*;
import javax.microedition.lcdui.*;
public class Connector extends Form implements CommandListener{
/**
* @param args
*/
private Command exitCommand;
private Connector_MIDlet cm;
private StringBuffer buf;
private InputStream is;
// TODO 自动生成方法存根
public Connector(Connector_MIDlet cm)
{
super("Content_HttpConnection");
this.cm=cm;
buf=new StringBuffer();
exitCommand=new Command("Exit",Command.EXIT,1);
this.addCommand(exitCommand);
this.setCommandListener(this);
try
{
//定义一个URL
String str_url="http://127.0.0.1:8086/default.htm";
System.out.println(str_url);
//定义一个ContentConnection
HttpConnection con=(HttpConnection) javax.microedition.io.Connector.open(str_url);
System.out.println(con);
int state=con.getResponseCode();
if(state==HttpConnection.HTTP_OK)
{
is=con.openInputStream();
}
int ch=0;
System.out.println("conType::"+con.getType());
System.out.println("conCode::"+con.getEncoding());
System.out.println("conLength::"+con.getLength());
System.out.println("is.length::"+is.available());
int tempLength=(int)con.getLength();
char bufChar[]=new char[tempLength];
InputStreamReader isr=new InputStreamReader(is);//运用包装类可以直接从字节输入流中读去字符
isr.read(bufChar,0,bufChar.length);
buf.append(String.valueOf(bufChar));
/*while((ch=is.read())!=-1)
{
buf.append((char)ch);
System.out.println((char)ch);
}*/
this.append(buf.toString());
// System.out.print("buf.lenght::");
// System.out.println(buf.toString().trim().length());//出去字符串缓存区的前导和尾补空白,或者得到的字符个数
//会和buf的长度不一致,这是StringBuffer本身的属性找成的..
is.close();
con.close();
//UnsatisfiedLinkError: isNetworkMonitorActive
}
catch(javax.microedition.io.ConnectionNotFoundException e)
{
System.out.println("urlCould not be foud");
}
catch(IOException ex)
{
System.out.println(ex.toString());
}
}
//定义响应按扭事件的函数
public void commandAction(Command c,Displayable d)
{
if(c==exitCommand)
{
cm.quitApp();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -