Archive for marzo, 2011


Codigo

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
/**
* @author Alonso Tukuch Cab
*/

public class URLMenu extends MIDlet implements CommandListener {
Display display = null;
List menu = null;
TextBox input = null;
Command ok;
Form pantalla;
private String url = «http://www.nohaytruco14.web44.net/Datos»; // la direccion de mi web host
private String url2 = «http://www.nohaytruco14.web44.net/Informacion»;
static final Command backCommand = new Command («Back», Command.BACK,0);
static final Command mainMenuCommand = new Command («Main», Command.SCREEN,1);
static final Command okCommand = new Command («Ver», Command.OK,2);
static final Command exitCommand = new Command («Exit», Command.STOP,3);
String currentMenu = null;

public URLMenu() {
display = Display.getDisplay(this);
}
public void startApp() throws MIDletStateChangeException {
display=Display.getDisplay(this);
menu = new List(«Menu Opcion», Choice.IMPLICIT);
menu.append(«Datos Generales», null);
menu.append(«Informacion», null);
menu.addCommand(okCommand);
menu.addCommand(exitCommand);
menu.setCommandListener(this);

mainMenu();
}
public void pauseApp(){
display = null;
menu = null;
input = null;
}
public void destroyApp(boolean unconditional){
notifyDestroyed();
}
void mainMenu() {
display.setCurrent(menu);
currentMenu = «Main»;
}
public void Datos() {
try{
download(url);
} catch (IOException e) {
System.out.println(«IOException: » + e);
}
}
public void Informacion() {
try{
download2(url2);
} catch (IOException e) {
System.out.println(«IOException: » + e);
}
}
public void testItem1() {
Datos();
currentMenu = «Datos Generales»;
}
public void testItem2() {
Informacion();
currentMenu = «Informacion»;
}
private void download (String url) throws IOException {

StringBuffer b = new StringBuffer();
InputStream is = null;
HttpConnection c = null;
TextBox t = null;

try {
long len = 0;
int ch =0;

c = (HttpConnection)Connector.open(url);
is = c.openInputStream();

while ((ch = is.read()) != -1) {
b.append((char)ch);
}

t = new TextBox («De internet», b.toString(), 1024, 0);
} finally {
if (is != null)
is.close();
if (c != null)
is.close();

}

display.setCurrent(t);
}
private void download2 (String url2) throws IOException {

StringBuffer b = new StringBuffer();
InputStream is = null;
HttpConnection c = null;
TextBox t = null;

try {
long len = 0;
int ch =0;

c = (HttpConnection)Connector.open(url);
is = c.openInputStream();

while ((ch = is.read()) != -1) {
b.append((char)ch);
}

t = new TextBox («De internet», b.toString(), 1024, 0);
} finally {
if (is != null)
is.close();
if (c != null)
is.close();

}

display.setCurrent(t);
}
public void commandAction(Command c, Displayable d) {
String label = c.getLabel();
if (label.equals(«Exit»)) {
destroyApp(true);
} else if (label.equals(«Back»)) {
if (currentMenu.equals(«Datos Generales») || currentMenu.equals(«Informacion»))
{
mainMenu();
}
} else {
List down = (List)display.getCurrent();
switch(down.getSelectedIndex()) {
case 0: testItem1();break;
case 1: testItem2();break;

}
}
}
}