Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
[email protected] webmail now available. Want one? Go here.
Cannot use outlook/hotmail/live here to register as they blocking our mail servers. #microsoftdeez
Obey the Epel!

Paste

Pasted as Java by Midlet ( 11 years ago )
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Timer;
import java.util.TimerTask;

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.Image;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class Midlet extends MIDlet implements CommandListener {

 private Display display;
 private String url = "http://google.com";
 private Form form;
 private Command exit;
 private Command stop;
 private Timer timer;
 private RunTimerTask tt;
 private int count = 0;
 boolean isPing = false;

 public Midlet() {

 }

 public void startApp() {
  display = Display.getDisplay(this);
  form = new Form("Ping");
  exit = new Command("Exit", Command.EXIT, 1);
  stop = new Command("Stop", Command.STOP, 2);

  form.addCommand(exit);
  form.addCommand(stop);
  form.setCommandListener(this);

  // Repeating every 1 minute
  timer = new Timer();
  tt = new RunTimerTask();
  timer.schedule(tt, 0, 1000 * 60);
  if (isPing) {
   timer.cancel();
  }
  display.setCurrent(form);
 }

 public void pauseApp() {
 }

 public void commandAction(Command c, Displayable d) {
  if (c == stop) {
   timer.cancel();
  } else if (c == exit) {
   try {
    destroyApp(false);
   } catch (MIDletStateChangeException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   notifyDestroyed();
  }
 }

 public static Image loadImage(String url) {
  HttpConnection connection = null;
  DataInputStream dis = null;
  byte[] data = null;

  try {
   connection = (HttpConnection) Connector.open(url);
   int length = (int) connection.getLength();
   data = new byte[length];
   dis = new DataInputStream(connection.openInputStream());
   dis.readFully(data);
   return Image.createImage(data, 0, data.length);
  } catch (Exception e) {
   System.out.println("Error LoadImage: " + e.getMessage());
   e.printStackTrace();
  } finally {
   if (connection != null)
    try {
     connection.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   if (dis != null)
    try {
     dis.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
  }

  return null;
 }

 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  // TODO Auto-generated method stub

 }

 /*--------------------------------------------------
  * RunTimerTask Class - Run the task
  *-------------------------------------------------*/

 private class RunTimerTask extends TimerTask {
  public final void run() {

   HttpConnection c = null;
   OutputStream os = null;
   InputStream is = null;
   int ch;
   StringBuffer b = new StringBuffer();
   try {
    c = (HttpConnection) Connector.open(url);

    // Set the request method and headers
    //c.setRequestMethod(HttpConnection.GET);
//    c.setRequestProperty("If-Modified-Since",
//      "7 Sep 2005 19:43:31 GMT");

//    c.setRequestProperty("User-Agent",
//      "Profile/MIDP-2.0 Configuration/CLDC-1.1");
//
//    c.setRequestProperty("Content-Language", "en-US");

    // Getting the output stream may flush the headers
 //    os = c.openOutputStream();
 //    os.write("Ping".getBytes());
 //    os.flush();

    int response = c.getResponseCode();
    System.out.println("response: " + response);
    if (response == HttpConnection.HTTP_OK) {
     is = c.openInputStream();
     while ((ch = is.read()) != -1) {
      b.append((char) ch);
     }
    }
    
    isPing = true;
    System.out.println("b: " + b);
    form.append("Successful ping");
   } catch (IOException e) {
    isPing = false;
    form.append("Pinging.....");
   } finally {
    try {
     is.close();
     os.close();
     c.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }

   }
  }
 }

}

 

Revise this Paste

Your Name: Code Language: