J2ME版HttpConnect

来源:互联网 发布:演技最好的电影 知乎 编辑:程序博客网 时间:2024/06/06 04:01

import java.io.DataInputStream;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.io.*;

public class HttpConnect extends MIDlet {

    public HttpConnect() {
        // 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

    }

    protected void startApp() throws MIDletStateChangeException{
        String URL = "http://192.168.0.114:8080/EngineService/sync?user=client";
        try{
        HttpConnection hpc = null;
        DataInputStream dis = null;
       
        boolean newline = false;
        String content = "";
       
        hpc = (HttpConnection)Connector.open(URL);
        hpc.setRequestMethod(HttpConnection.GET);
        dis = new DataInputStream(hpc.openInputStream());
       
        int character;
        while((character = dis.read())!= -1){
            if((char)character=='//'){
                newline=true;
                continue;
            }else{
                if((char)character=='n'&&newline){
                    content += "/n";
                    newline = false;
                }else if(newline){
                    content += "//"+(char)character;
                    newline = false;
                }else{
                    content +=(char)character;
                    newline = false;
                }
            }
           
        }
       
        hpc.close();
        hpc = null;
        dis.close();
        dis = null;
       
        content = content.trim();
        System.out.println(content);
       
       
        }catch(Exception e){
            System.out.println("Error:"+e);
        }
    }

}