package com.RayiDream;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
public class DrawData extends Canvas {
String str;
public DrawData() {
str = getTextByUTF("http://127.0.0.1:8088/data.txt");
}
//------------绘制数据-------------------------------------------------------
protected void paint(Graphics g) {
System.out.println("22222");
g.setColor(0xFFFFFF);
g.fillRect(0, 0, getWidth(), getHeight());//清屏
g.setColor(0xFF0000);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_LARGE));
g.drawString(str, getWidth() / 2, getHeight() / 2, Graphics.TOP //绘制
| Graphics.HCENTER);
}
//-----------读取数据-------------------------------------------------------
public String getTextByUTF(String url) {
String strReturn = "";
int ic;
InputStream in = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
byte[] myData;
byte[] buffer = new byte[1024];
try {
HttpConnection c = (HttpConnection) Connector.open(url);
in = c.openInputStream();
if (in != null) {
while ((ic = in.read(buffer)) > 0) {
dos.write(buffer, 0, ic);
}
myData = baos.toByteArray();
strReturn = new String(myData);
in.close();
}
dos.close();
baos.close();
} catch (Exception e) {
System.out.println("读取悲剧了....");
} finally {
in = null;
dos = null;
baos = null;
}
return strReturn;
}
}
=====================================华丽的分割线=========================================
package com.RayiDream;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class GetData extends MIDlet {
public GetData() {
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(new DrawData());
}
}
运行结果: