程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> J2ME 使用HTTP網絡連接實現排行榜

J2ME 使用HTTP網絡連接實現排行榜

編輯:J2ME

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;
       }

 }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved