(源自:http://hi.baidu.com/wishwingliao/blog/item/58d21e4ccaeddcf9d62afc69.html)
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class LoadText extends MIDlet implements Runnable,CommandListener
{
private Display display;
private Thread textThread;
private Form mainForm,textForm;
private TextField textField;
public void startApp()
{
this.display = Display.getDisplay(this);
this.mainForm = new Form("慢慢等吧,别急...");
this.textForm = new Form("文本");
this.textField = new TextField("靠自己","hello",1024,TextField.ANY);
this.textForm.addCommand(new Command("离开", Command.EXIT, 0));
this.textForm.setCommandListener(this);
this.display.setCurrent(mainForm);
this.textThread = new Thread(this);
this.textThread.start();//启动线程,会自动调用Runnable接口的run()方法
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command c, Displayable s)
{
this.notifyDestroyed();
}
public void run()
{
try
{
String URL = "http://www.yiyim.com/text/kaoziji.txt";
String text = loadText(URL); //获取服务器文本
this.textField.setString(text);
}
catch (IOException ioe)
{
System.out.println("出错了:" + ioe.toString());
}
textForm.append(textField);
display.setCurrent(textForm);
}
public String loadText(String url) throws IOException
{
HttpConnection hpc = null;
DataInputStream dis = null;
try
{
//打开一个连接返回一个Connection接口类的对象,
//之后转型为HttpConnection接口类的对象
hpc = (HttpConnection)Connector.open(url);
int length = (int)hpc.getLength();//获取数据长度
byte[] data = new byte[length];//创建一个字节数组对象
//openInputStream()打开并返回一个输入流对象
//DataInputStream类对象:
//数据输入流允许应用程序以与机器无关方式从基础输入流中读取基本 Java 数据类型。
//应用程序可以使用数据输出流写入稍后由数据输入流读取的数据。
dis = new DataInputStream(hpc.openInputStream());
//从输入流(dis)中读取一些字节,并将它们存储到缓冲区数组 data中。
//读取的字节数等于 data 的长度
dis.readFully(data);
//构造一个新的 String,方法是使用平台的默认字符集解码字节的指定数组。
//新的 String 的长度是一个字符集函数,因此不能等于字节数组的长度。
//当给定字节在给定字符集中无效的情况下,该构造方法无指定的行为。
//public String(byte[] bytes)
//bytes - 要解码为字符的字节
return new String(data);
}
finally
{
//close()方法用于关闭连接
//当连接被关闭后,除了该方法外,其他任何方法的调用都会
//抛出IOException异常
//关闭一个已经关闭的连接不会产生任何影响
if(hpc != null) hpc.close();
if(dis != null) dis.close();
}
}
}
====================================================================================
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class WEBtext extends MIDlet implements Runnable,CommandListener
{
private Display display;
private Thread textThread;
private Form mainForm,textForm;
private TextField textField;
public void startApp()
{
this.display = Display.getDisplay(this);
this.mainForm = new Form("慢慢等吧,别急...");
this.textForm = new Form("文本");
this.textField = new TextField("靠自己","hello",1024,TextField.ANY);
this.textForm.addCommand(new Command("离开", Command.EXIT, 0));
this.textForm.setCommandListener(this);
this.display.setCurrent(mainForm);
this.textThread = new Thread(this);
this.textThread.start();
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command c, Displayable s)
{
this.notifyDestroyed();
}
public void run()
{
try
{
String URL = "http://www.yiyim.com/text/kaoziji.txt";
String text = loadText(URL); //获取服务器文本
this.textField.setString(text);
}
catch (IOException ioe)
{
System.out.println("出错了:" + ioe.toString());
}
textForm.append(textField);
display.setCurrent(textForm);
}
public String loadText(String url) throws IOException
{
HttpConnection hpc = null;
DataInputStream dis = null;
try
{
hpc = (HttpConnection)Connector.open(url);
int length = (int)hpc.getLength();
byte[] data = new byte[length];
// dis = new DataInputStream(hpc.openInputStream());//这种方式也是可以的
dis = hpc.openDataInputStream();
dis.readFully(data);
return new String(data);
}
finally
{
if(hpc != null) hpc.close();
if(dis != null) dis.close();
}
}
}
====================================================================================
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class loadtext extends MIDlet implements Runnable,CommandListener
{
private Display display;
private Thread textThread;
private Form mainForm,textForm;
private TextField textField;
public void startApp()
{
this.display = Display.getDisplay(this);
this.mainForm = new Form("加载中...");
this.textForm = new Form("文本");
this.textField = new TextField("靠自己","hello",1024,TextField.ANY);
this.textForm.addCommand(new Command("离开", Command.EXIT, 0));
this.textForm.setCommandListener(this);
this.display.setCurrent(mainForm);
this.textThread = new Thread(this);
this.textThread.start();
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command c, Displayable s)
{
this.notifyDestroyed();
}
public void run()
{
try
{
String URL = "http://www.yiyim.com/text/kaoziji.txt";
String text = loadText(URL); //获取服务器文本
this.textField.setString(text);
}
catch (IOException ioe)
{
System.out.println("出错了:" + ioe.toString());
}
textForm.append(textField);
display.setCurrent(textForm);
}
public String loadText(String url) throws IOException
{
HttpConnection hpc = null;
DataInputStream dis = null;
try
{
hpc = (HttpConnection)Connector.open(url);
int length = (int)hpc.getLength();
byte[] data = new byte[length];
hpc = null;
// dis = new DataInputStream(hpc.openInputStream());//这种方式也是可以的
// dis = hpc.openDataInputStream();
dis = Connector.openDataInputStream(url);//这种方式也是可以的
dis.read(data);
return new String(data);
}
finally
{
if(hpc != null) hpc.close();
if(dis != null) dis.close();
}
}
}
====================================================================================
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class WEBtext extends MIDlet implements Runnable,CommandListener
{
private Display display;
private Thread textThread;
private Form mainForm,textForm;
private TextField textField;
public void startApp()
{
this.display = Display.getDisplay(this);
this.mainForm = new Form("加载中...");
this.textForm = new Form("文本");
this.textField = new TextField("靠自己","hello",1024,TextField.ANY);
this.textForm.addCommand(new Command("离开", Command.EXIT, 0));
this.textForm.setCommandListener(this);
this.display.setCurrent(mainForm);
this.textThread = new Thread(this);
this.textThread.start();
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command c, Displayable s)
{
this.notifyDestroyed();
}
public void run()
{
try
{
String URL = "http://www.yiyim.com/text/kaoziji.txt";
String text = loadText(URL); //获取服务器文本
this.textField.setString(text);
}
catch (IOException ioe)
{
System.out.println("出错了:" + ioe.toString());
}
textForm.append(textField);
display.setCurrent(textForm);
}
public String loadText(String url) throws IOException
{
HttpConnection hpc = null;
InputStream is = null;
try
{
hpc = (HttpConnection)Connector.open(url);
int length = (int)hpc.getLength();
byte[] data = new byte[length];
hpc = null;
is = Connector.openInputStream(url);//这种方式也是可以的
is.read(data);
return new String(data);
}
finally
{
if(hpc != null) hpc.close();
if(dis != null) dis.close();
}
}
}
====================================================================================