U E D R , A S I H C RSS

Java2 Micro Edition/Midp Http Connection Example

No older revisions available

No older revisions available



Spike2.java


~cpp 
package com.minnysunny.mobilerssreader.spike;

import javax.microedition.midlet.*;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.*;

public class Spike2 extends MIDlet implements CommandListener {
    private Display display;
    private TextBox tb;
    private Command exit;
    private HttpConnection httpConn;
    
    public Spike2() {
        SpikeGetHtml sgh = new SpikeGetHtml("http://dduk.idaizy.com/j2me/test.txt");
        String str = sgh.getContent2();
        sgh.cleanUp();
        
        display = Display.getDisplay(this);
        exit = new Command("EXIT", Command.EXIT, 0);
        //tb = new TextBox("Example", "HelloWorld", 20, TextField.ANY);
        tb = new TextBox("Example", str, 20, TextField.ANY);
        tb.addCommand(exit);
        tb.setCommandListener(this);
    }
    
    protected void startApp() throws MIDletStateChangeException {
        display.setCurrent(tb);
    }

    protected void pauseApp() {
    }

    protected void destroyApp(boolean b) {
    }

    public void commandAction(Command c, Displayable d) {
        if( c == exit ) {
            destroyApp(false);
            notifyDestroyed();
        }
    }
}
 

SpikeGetHtml.java


~cpp 

package com.minnysunny.mobilerssreader.spike;

import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

public class SpikeGetHtml {
    private HttpConnection httpConn;
    private DataInputStream dis;
    
    public SpikeGetHtml(String url) {
        try {
            init(url);
        } catch(IOException ex) {
            ex.printStackTrace();
        }
    }
    
    private void init(String url) throws IOException {
        httpConn = (HttpConnection)Connector.open(url);
        httpConn.setRequestMethod(HttpConnection.GET);
        httpConn.setRequestProperty("User-Agent", "Mozilla/4.0");
        dis = httpConn.openDataInputStream();
    }
    
    public void cleanUp() {
        try { 
	        if( dis != null ) {
	            dis.close();
	            dis = null;
	        }
	        if( httpConn != null ) {
	            httpConn.close();
	            httpConn = null;
	        }
        } catch(IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public String getContent() {
        String ret = "";
        try {
            for(int i=0; i<4; ++i) {
                byte b = dis.readByte();
                ret += (char)b;
            }
        } catch(IOException ex) {
            ex.printStackTrace();
        }
        return ret;
    }
    
    public String getContent2() {
        String ret = null;
        try {
            byte buffer[] = new byte[10];
            dis.read(buffer);
            ret = new String(buffer);
        } catch(IOException ex) {
            ex.printStackTrace();
        }
        return ret;
    }
}
 

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:30
Processing time 0.0149 sec