j2me 读 servlet 所有数据

package http;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class HttpServlet2 extends MIDlet implements CommandListener{

    private Display dis;
    private Form frm=new Form("网页信息为");  //记住new
    private TextField tf=new TextField("输入地址","",50,TextField.ANY);
    private Command submit=new Command("登陆",Command.SCREEN,1);
   
    public HttpServlet2() {
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    }

    protected void pauseApp() {
    }

    protected void startApp() throws MIDletStateChangeException {
        dis=Display.getDisplay(this);
        dis.setCurrent(frm);
        frm.addCommand(submit);
        frm.append(tf);
        frm.setCommandListener(this);
    }

    public void commandAction(Command c, Displayable d) {
        if(c==submit){
        ThreadDemo td=new ThreadDemo();
        td.start();
           
        }
       
    }
   
    class ThreadDemo extends Thread{
        public void run(){
            try {
                HttpConnection hc=(HttpConnection)Connector.open("http://localhost:8080/lbs/mobile_terminal?"
                        +"TS=1234567891&UserID=10226&Cmd=GetMyGifts&StartIndex=1&Count=5");
                DataInputStream di=hc.openDataInputStream();
                ByteArrayOutputStream bos=new ByteArrayOutputStream(); //把数据以int的形式读到数组中,再构成字符串
                int temp=di.read();
                while(temp!=-1){   
                    bos.write(temp);
                     temp=di.read();
                }
                byte[] bb=bos.toByteArray();
                String content=new String(bb,"utf-8");
                  System.out.println(content);
                   frm.append(content);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
       
    }

}

你可能感兴趣的:(C++,c,servlet,C#,mobile)