J2ME 使用HTTP网络连接实现排行榜
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class MIDLET extends MIDlet implements CommandListener {
private Command show=new Command("查看排行榜",Command.SCREEN,1);
private Form frm=new Form("排行榜");
String str=null;
public Display dis;
private TopHttp th=new TopHttp();
public MIDLET() {
// TODO Auto-generated constructor stub
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
public void Chang(String frmname)
{
if(frmname.equals("list"))
{
Display.getDisplay(this).setCurrent(th);
}
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
frm.addCommand(show);
frm.setCommandListener(this);
Display.getDisplay(this).setCurrent(frm);
// Display.getDisplay(this).setCurrent(th);
/*
try {
javax.microedition.io.HttpConnection hc=(HttpConnection) Connector.open("http://221.130.10.156:8080/requestinfo.aspx?name=liming&integral=120");
System.out.println(hc.getResponseMessage());
DataInputStream dis= hc.openDataInputStream();
byte[] b=new byte[(int) hc.getLength()];
dis.read(b);
String str=new String(b);
System.out.println(str.substring(0,str.indexOf("<html>")));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
*/
}
public void commandAction(Command c, Displayable d) {
// TODO Auto-generated method stub
if(c==show)
{
//Display.getDisplay(this).setCurrent(th);
Thread t=new Thread(th);
t.start();
}
}
/*
public class TopHttp extends List implements Runnable
{
private MIDLET mi;
public TopHttp(MIDLET m) {
super("排行榜前10名", List.IMPLICIT);
// TODO Auto-generated constructor stub
this.mi=m;
}
public void run() {
// TODO Auto-generated method stub
javax.microedition.io.HttpConnection hc = null;
try {
hc = (HttpConnection) Connector.open("http://221.130.10.156:8080/default.aspx");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
System.out.println(hc.getResponseMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DataInputStream dis = null;
try {
dis = hc.openDataInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] b=new byte[(int) hc.getLength()];
try {
dis.read(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
str=new String(b);
System.out.println(str.substring(0,str.indexOf("<html>")));
String[] strs=split(str.substring(0,str.indexOf("<html>")), "$");
for(int i=0;i<strs.length;i++)
{
String[] s=split(strs[i],"&");
for(int j=0;j<s.length;j++){
this.append((String)s[j],null);
System.out.println((String)s[j]);
}
}
mi.Chang("list");
}
}
*/
public class TopHttp extends GameCanvas implements Runnable
{
private MIDLET mi;
private int x=10;
private int y=5;
int z=5;
protected TopHttp() {
super(true);
// TODO Auto-generated constructor stub
}
Graphics gra=this.getGraphics();
public void run() {
Font font= Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_BOLD,Font.SIZE_LARGE);
gra.setFont(font);
gra.setColor(0,0,0);
gra.drawRect(0, 0, this.getWidth(), this.getHeight());
// TODO Auto-generated method stub
javax.microedition.io.HttpConnection hc = null;
try {
hc = (HttpConnection) Connector.open("http://221.130.197.156:8678/default.aspx");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
System.out.println(hc.getResponseMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DataInputStream dis = null;
try {
dis = hc.openDataInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] b=new byte[(int) hc.getLength()];
try {
dis.read(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
str=new String(b);
String splitstr=str.substring(0,str.indexOf("<html>"));
if(splitstr.length()>0){
String[] strs=split(splitstr, "$");
System.out.println(strs.length);
System.out.println(splitstr);
for(int i=0;i<strs.length;i++)
{
y+=gra.getFont().getHeight()+10;
String[] s=split(strs[i],"&");
if(s.length>1){
x=10;
for(int j=0;j<s.length;j++){
//this.append((String)s[j],null);
try{
//gra.drawLine(x,y+gra.getFont().getHeight()+10, 100, 100);
x+=gra.getFont().getSize()*2+5;
gra.drawString((i+1)+" "+(String)s[j]+" ", x,y , Graphics.HCENTER|Graphics.TOP);
this.flushGraphics();
}catch(Exception ex)
{
System.out.println(ex.getMessage().toString());
}
}
//System.out.println("ok");
}
}
}
Chang("list");
}
}
public static String[] split(String original,String regex)
{
//取子串的起始位置
int startIndex = 0;
//将结果数据先放入Vector中 注意应当引入
Vector v = new Vector();
//返回的结果字符串数组
String[] str = null;
//存储取子串时起始位置
int index = 0;
//获得匹配子串的位置
startIndex = original.indexOf(regex);
//如果起始字符串的位置小于字符串的长度,则证明没有取到字符串末尾。
//-1代表取到了末尾
//判断的条件,循环查找依据
while(startIndex < original.length() && startIndex != -1) {
String temp = original.substring(index,startIndex);
//取子串
v.addElement(temp);
//设置取子串的起始位置
index = startIndex + regex.length();
//获得匹配子串的位置
startIndex = original.indexOf(regex,startIndex + regex.length());
}
//取结束的子串
v.addElement(original.substring(index + 1 - regex.length()));
//将Vector对象转换成数组
str = new String[v.size()];
for(int i=0;i<v.size();i++)
str[i] =(String)v.elementAt(i);
//返回生成的数组
return str;
}
}