📄 readfromhttpserver.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;
import java.util.*;
public class ReadFromHTTPServer extends MIDlet implements CommandListener
{
private Command exitCommand,nextCommand;
private Display display;
private Form form;
private StringItem keyWord;
private byte[] b;
public ReadFromHTTPServer()
{
display = Display.getDisplay(this);
exitCommand = new Command("Exit",Command.EXIT,2);
nextCommand = new Command("Next",Command.OK,2);
form = new Form("Online Novel");
keyWord = new StringItem(" "," ");
Ticker ticker = new Ticker("Download Novel");
form.setTicker(ticker);
form.append(keyWord);
form.addCommand(exitCommand);
form.addCommand(nextCommand);
form.setCommandListener(this);
}
public void startApp() throws MIDletStateChangeException
{
display.setCurrent(form);
readKeyword();
showKeyword();
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){}
public void commandAction(Command c, Displayable d)
{
if(c==exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
else if(c==nextCommand)
{
showKeyword();
}
}
private void readKeyword()
{
HttpConnection connect = null;
InputStream inStream = null;
ByteArrayOutputStream baos=new ByteArrayOutputStream();
try
{
//建立并打开HTTP连接
connect = (HttpConnection)Connector.open("http://localhost:8080/note.txt");
//获得连接的输入流
inStream = connect.openInputStream();
int input;
//通过输入流读取服务器上文件
while ((input=inStream.read())!= -1)
{
baos.write(input);
}
//将读取的内容转化为字节数组
b=baos.toByteArray();
}
catch(IOException e)
{
System.err.println(" the connection could not be established. sorry for the inconvenience");
}
}
private void showKeyword()
{
try
{
//将读取的文件内容转换为中文格式并显示出来
keyWord.setText(new String(b,"gb2312"));
}
catch(Exception e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -