@author Savio
*/
public class HelloMidlet extends MIDlet implements CommandListener, ItemCommandListener {
/** Creates a new instance of HelloMidlet */
public HelloMidlet() {
}
public String RS_NAME = “dados”;
public RecordStore rs = null;
private static String url = “http://dam.ddns.com.br:8084/testMeServer/MathServlet”;
//private static String url = “http://casadopeixe:8084/testMeServer/MathServlet”;
public String nome="";
public String idade="";
private Form helloForm;
private Command exitCommand;
private TextField tnome;
private TextField tidade;
private StringItem label;
private Command CGravar;
private Command CExportar;
private TextField tpesquisa;
private Command CPesquisar;
private Command CExcluir;
private Command Cenviar;
Exportando2 Exportando2;
/** Called by the system to indicate that a command has been invoked on a particular item.
-
@param command the Command that ws invoked
-
@param item the Item on which the command was invoked
*/
public void commandAction(Command command, Item item) {
// Insert global pre-action code here
if (item == tpesquisa) {
if (command == CPesquisar) {
// Insert pre-action code here
// Do nothing
// Insert post-action code here
open();
PesqId2( Integer.parseInt(tpesquisa.getString()) );
close();
}
}
// Insert global post-action code here
}
/** This method initializes UI of the application.
*/
private void initialize() {
// Insert pre-init code here
getDisplay().setCurrent(get_helloForm());
// Insert post-init code here
}
/** Called by the system to indicate that a command has been invoked on a particular displayable.
-
@param command the Command that ws invoked
-
@param displayable the Displayable on which the command was invoked
*/
public void commandAction(Command command, Displayable displayable) {
// Insert global pre-action code here
if (displayable == helloForm) {
if (command == exitCommand) {
// Insert pre-action code here
exitMIDlet();
// Insert post-action code here
} else if (command == CGravar) {
// Insert pre-action code here
// Do nothing
// Insert post-action code here
open();
Gravar(tnome.getString(),tidade.getString());
close();
} else if (command == CExportar) {
// Insert pre-action code here
// Do nothing
// Insert post-action code here
//open();
//IniciarExportacao();
} else if (command == CExcluir) {
// Insert pre-action code here
// Do nothing
// Insert post-action code here
open();
delete(cod);
close();
} else if (command == Cenviar) {
// Insert pre-action code here
// Do nothing
// Insert post-action code here
Exportando2= new Exportando2(this);
Exportando2.start();
}
}
// Insert global post-action code here
}
/**
- This method should return an instance of the display.
*/
public Display getDisplay() {
return Display.getDisplay(this);
}
/**
- This method should exit the midlet.
*/
public void exitMIDlet() {
getDisplay().setCurrent(null);
destroyApp(true);
notifyDestroyed();
}
/** This method returns instance for helloForm component and should be called instead of accessing helloForm field directly.
-
@return Instance for helloForm component
*/
public Form get_helloForm() {
if (helloForm == null) {
// Insert pre-init code here
helloForm = new Form(null, new Item[] {
get_tnome(),
get_tidade(),
get_label(),
get_tpesquisa()
});
helloForm.addCommand(get_exitCommand());
helloForm.addCommand(get_CGravar());
helloForm.addCommand(get_CExportar());
helloForm.addCommand(get_CExcluir());
helloForm.addCommand(get_Cenviar());
helloForm.setCommandListener(this);
// Insert post-init code here
}
return helloForm;
}
/** This method returns instance for exitCommand component and should be called instead of accessing exitCommand field directly.
-
@return Instance for exitCommand component
*/
public Command get_exitCommand() {
if (exitCommand == null) {
// Insert pre-init code here
exitCommand = new Command(“Exit”, Command.EXIT, 1);
// Insert post-init code here
}
return exitCommand;
}
/** This method returns instance for tnome component and should be called instead of accessing tnome field directly.
-
@return Instance for tnome component
*/
public TextField get_tnome() {
if (tnome == null) {
// Insert pre-init code here
tnome = new TextField(“Nome”, null, 120, TextField.ANY);
// Insert post-init code here
}
return tnome;
}
/** This method returns instance for tidade component and should be called instead of accessing tidade field directly.
-
@return Instance for tidade component
*/
public TextField get_tidade() {
if (tidade == null) {
// Insert pre-init code here
tidade = new TextField(“Idade”, null, 120, TextField.ANY);
// Insert post-init code here
}
return tidade;
}
/** This method returns instance for label component and should be called instead of accessing label field directly.
-
@return Instance for label component
*/
public StringItem get_label() {
if (label == null) {
// Insert pre-init code here
label = new StringItem("", “”);
// Insert post-init code here
}
return label;
}
/** This method returns instance for CGravar component and should be called instead of accessing CGravar field directly.
-
@return Instance for CGravar component
*/
public Command get_CGravar() {
if (CGravar == null) {
// Insert pre-init code here
CGravar = new Command(“Gravar”, Command.OK, 1);
// Insert post-init code here
}
return CGravar;
}
/** This method returns instance for CExportar component and should be called instead of accessing CExportar field directly.
-
@return Instance for CExportar component
*/
public Command get_CExportar() {
if (CExportar == null) {
// Insert pre-init code here
CExportar = new Command(“Exportar”, Command.OK, 1);
// Insert post-init code here
}
return CExportar;
}
/** This method returns instance for tpesquisa component and should be called instead of accessing tpesquisa field directly.
-
@return Instance for tpesquisa component
*/
public TextField get_tpesquisa() {
if (tpesquisa == null) {
// Insert pre-init code here
tpesquisa = new TextField(“Entre com o C\u00F3digo”, null, 120, TextField.ANY);
tpesquisa.addCommand(get_CPesquisar());
tpesquisa.setItemCommandListener(this);
// Insert post-init code here
}
return tpesquisa;
}
/** This method returns instance for CPesquisar component and should be called instead of accessing CPesquisar field directly.
-
@return Instance for CPesquisar component
*/
public Command get_CPesquisar() {
if (CPesquisar == null) {
// Insert pre-init code here
CPesquisar = new Command(“Pesquisar”, Command.OK, 1);
// Insert post-init code here
}
return CPesquisar;
}
/** This method returns instance for CExcluir component and should be called instead of accessing CExcluir field directly.
-
@return Instance for CExcluir component
*/
public Command get_CExcluir() {
if (CExcluir == null) {
// Insert pre-init code here
CExcluir = new Command(“Excluir”, Command.OK, 1);
// Insert post-init code here
}
return CExcluir;
}
/** This method returns instance for Cenviar component and should be called instead of accessing Cenviar field directly.
-
@return Instance for Cenviar component
*/
public Command get_Cenviar() {
if (Cenviar == null) {
// Insert pre-init code here
Cenviar = new Command(“Enviar Param”, Command.OK, 1);
// Insert post-init code here
}
return Cenviar;
}
private int cod;
public void close() {
try {
rs.closeRecordStore();
}
catch (RecordStoreNotOpenException e) {
System.out.println("-- O Record Store esta fechado");
}
catch (RecordStoreException e) {
System.out.println("-- Outro erro");
}
}
public void open() {
try {
//rs.deleteRecordStore(RS_NAME);
rs = RecordStore.openRecordStore(RS_NAME, true);
}
catch (RecordStoreNotFoundException e) {
System.out.println("-- RecordStore inexistente");
}
catch (RecordStoreException e) {
System.out.println("-- Outro erro");
}
}
public void Gravar(String nome,String idade) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeUTF(nome);
dos.writeUTF(idade);
dos.flush();
byte[] data = baos.toByteArray();
int id = rs.addRecord(data, 0, data.length);
//int id = this.getRecordStore().addRecord(this.data,0, data.length);
label.setText(" “+String.valueOf(id));
tnome.setString(”");
tidade.setString("");
baos.close();
dos.close();
}
catch (IOException e) {
label.setText("-- Erro de IO");
}
catch (RecordStoreFullException e) {
label.setText("-- Não existe espaço disponível");
}
catch (RecordStoreNotOpenException e) {
label.setText("-- O Record Store esta fechado");
}
catch (RecordStoreException e) {
label.setText("-- Outro erro");
}
}
public void delete(int id) {
try {
rs.deleteRecord(id);
}
catch (RecordStoreFullException e) {
System.out.println("-- Não existe espaço disponível");
}
catch (RecordStoreNotOpenException e) {
System.out.println("-- O Record Store esta fechado");
}
catch (RecordStoreException e) {
System.out.println("-- Outro erro");
}
}
public String PesqId(int id) {
String toReturn = “”;
try {
int recordSize = rs.getRecordSize(id);
byte[] data = new byte[recordSize];
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
int numBytes = rs.getRecord(id, data, 0);
nome = dis.readUTF();
idade = dis.readUTF();
toReturn = nome+"|"+idade;
//ticker1.setString(“Pré-Venda Nº: 0”+dis.readUTF()+" Mesa Nº: 0"+dis.readUTF());
bais.reset();
bais.close();
dis.close();
}
catch (IOException e) {
System.out.println("-- Erro de IO");
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("-- Registro muito grande");
}
catch (InvalidRecordIDException e) {
System.out.println("-- ID inexistente");
}
catch (RecordStoreNotOpenException e) {
System.out.println("-- O Record Store esta fechado");
}
catch (RecordStoreException e) {
System.out.println("-- Outro erro");
}
return toReturn;
}
public String PesqId2(int id) {
String toReturn = “”;
try {
int recordSize = rs.getRecordSize(id);
cod=id;
byte[] data = new byte[recordSize];
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
int numBytes = rs.getRecord(id, data, 0);
tnome.setString( dis.readUTF() ) ;
tidade.setString( dis.readUTF() );
toReturn = tnome.getString()+"|"+tidade.getString();
//ticker1.setString(“Pré-Venda Nº: 0”+dis.readUTF()+" Mesa Nº: 0"+dis.readUTF());
bais.reset();
bais.close();
dis.close();
}
catch (IOException e) {
label.setText("-- Erro de IO");
toReturn=“erro”;
}
catch (ArrayIndexOutOfBoundsException e) {
label.setText("-- Registro muito grande");
toReturn=“erro”;
}
catch (InvalidRecordIDException e) {
label.setText("-- ID inexistente");
toReturn=“erro”;
}
catch (RecordStoreNotOpenException e) {
label.setText("-- O Record Store esta fechado");
toReturn=“erro”;
}
catch (RecordStoreException e) {
label.setText("-- Outro erro");
toReturn=“erro”;
}
return toReturn;
}
public void limpar(){
try {
Thread.sleep(2500);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
label.setText("");
tnome.setString("");
tidade.setString("");
}
public void IniciarExportacao() {
try {
for (int i = 1; i <= rs.getNumRecords(); i++) {
PesqId(i);
}
close();
}
catch (RecordStoreNotOpenException ex) {
System.out.println("-- O Record Store esta fechado");
}
}
public void startApp() {
initialize();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public class Exportando2 implements Runnable {
HelloMidlet midlet;
private Display display;
private int i=0;
//private GravarPrevenda GravarPrevenda ;
public Exportando2(HelloMidlet midlet) {
this.midlet = midlet;
display = Display.getDisplay(midlet);
}
public void start() {
Thread t = new Thread(this);
t.start();
}
public void run() {
String param="";
int t=0;
try {
//------------------------------------------------------
StringBuffer sb = new StringBuffer();
HttpConnection c = null;
c = (HttpConnection) Connector.open(url);
c.setRequestProperty(
“User-Agent”,“Profile/MIDP-2.0, Configuration/CLDC-1.1”);
c.setRequestProperty(“Content-Language”,“en-US”);
c.setRequestMethod(HttpConnection.GET);
DataOutputStream dos = (DataOutputStream) c.openDataOutputStream();
dos.writeUTF(“300”);
dos.writeUTF(“TESTE”);
dos.writeUTF(“TESTE”);
dos.flush();
dos.close();
DataInputStream is =(DataInputStream) c.openDataInputStream();
int ch;
sb = new StringBuffer();
while ((ch = is.read()) != -1) {
sb.append((char)ch);
}
param=sb.toString();
is.close();
c.close();
label.setText("Operação bem sucedida.");
limpar();
} catch (Exception ex) {
label.setText("Houve erro.");
limpar();
label.setText("Tente novamente.");
limpar();
ex.printStackTrace();
}
}