Program to connect to a URL and print its response code in J2ME

/*
 * Package: test
 * FileName: WebSite.java
 *
 * Created by pandian on Jun 15, 2010 11:13:57 AM
 */
package test;

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;

/**
 * @author pandian
 *
 */
public class WebSite extends MIDlet implements CommandListener, Runnable {
 private Display display;
 private Form form;
 private TextField url;
 private Command go;
 private Command Cancel;
 private int responseCode;

 /**
 * Instantiates a new web site.
 */
 public WebSite() {
 super();
 form = new Form("Website");
 this.url = new TextField("URL :", "http://blog.grassfield.org", 30, TextField.URL);
 this.go = new Command("Go", Command.OK, 2);
 this.Cancel = new Command("Cancel", Command.CANCEL, 2);
 }

 /* (non-Javadoc)
 * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
 */
 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
 super.notifyDestroyed();

 }

 /* (non-Javadoc)
 * @see javax.microedition.midlet.MIDlet#pauseApp()
 */
 protected void pauseApp() {

 }

 /* (non-Javadoc)
 * @see javax.microedition.midlet.MIDlet#startApp()
 */
 protected void startApp() throws MIDletStateChangeException {
 this.display = Display.getDisplay(this);
 form.append(this.url);
 form.addCommand(this.go);
 form.addCommand(this.Cancel);
 form.setCommandListener(this);
 this.display.setCurrent(form);

 }

 /* (non-Javadoc)
 * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
 */
 public void commandAction(Command c, Displayable d) {
 String label = c.getLabel();
 if (label.equals("Go")){
 this.openSite(this.url.getString());
 }else{
 try {
 this.destroyApp(true);
 } catch (MIDletStateChangeException e) {
 e.printStackTrace();
 }
 }

 }

 /**
 * Open site.
 *
 * @param url the url
 */
 private void openSite(String url) {
 System.out.println("open url:"+url);
 System.out.println("1");
 Thread t = new Thread(this);
 t.start();
 this.form.append("Response Code:"+responseCode+"\n");
 this.display.setCurrent(this.form);

 }

 /* (non-Javadoc)
 * @see java.lang.Runnable#run()
 */
 public void run() {
 try {
 this.responseCode=0;
 System.out.println("---1---");
 HttpConnection c = (HttpConnection) Connector.open(url.getString());
 System.out.println("---2---");
 this.responseCode = c.getResponseCode();
 System.out.println("---3---"+responseCode);
 this.form.append("Host:"+c.getHost()+"\n");
 this.form.append("Content Length:"+c.getLength()+"\n");
 c.close();
 System.out.println("---4---");
 } catch (IOException e) {
 e.printStackTrace();
 }

 }

}

Comments are closed.