Hello, I am new in this area, I ty to develope J2ME program in NetBeans 5.5 that connect by socket and I send data, my program send the data perfectly but when I try to recieve the data it seems to be that the telephone movil disconect, I send and recieve data 4 time.
I use Visual MIDLET to make the J2ME program in NetBeans 5.5, here my code:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;
public class J2MESocket extends MIDlet implements CommandListener {
/** Creates a new instance of J2MESocket */
public J2MESocket() {
initialize();
}
private Form form1;
private Command CmdSend;
private Command CmdClose;
private TextField TxtTexto;
private StringItem StrItmRespuesta;
private String StrIp = "";
private String StrPort = "";
private String StrEnviarCampos = "";
private String StrValue = "";
public String getStrIp() {
StrIp = "220.168.292.150";
return StrIp;
}
public String getStrPort() {
StrPort = "5002";
return StrPort;
}
public String getStrEnviarCampos() {
return StrEnviarCampos;
}
public void setStrValue(String StrValue) {
this.StrValue = StrValue;
StrItmRespuesta.setText(StrValue);
}
/**
* utility method to show modal alerts
*/
public void showMessage(String title, String txt){
Alert a = new Alert(title);
a.setString(txt);
a.setTimeout(Alert.FOREVER);
Display.getDisplay(this).setCurrent(a);
}
/** 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 == form1) {
if (command == CmdSend) {
// Insert pre-action code here
// Do nothing
// Insert post-action code here
StrEnviarCampos = TxtText.getString();
ChannelComunication ChannelComunication = new ChannelComunication(this);
ChannelComunication.start();
ChannelComunication = null;
Runtime.getRuntime().gc();
} else if (command == CmdClose) {
// Insert pre-action code here
// Do nothing
// Insert post-action code here
exitMIDlet();
}
// Insert global post-action code here
}
/** This method initializes UI of the application.
*/
private void initialize() {
// Insert pre-init code here
getDisplay().setCurrent(get_form1());
// Insert post-init 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 form1 component and should be called instead of accessing form1 field directly.
* @return Instance for form1 component
*/
public Form get_form1() {
if (form1 == null) {
// Insert pre-init code here
form1 = new Form(null, new Item[] {
get_TxtTexto(),
get_StrItmRespuesta()
});
form1.addCommand(get_CmdSend());
form1.addCommand(get_CmdClose());
form1.setCommandListener(this);
// Insert post-init code here
}
return form1;
}
/** This method returns instance for CmdSend component and should be called instead of accessing CmdSend field directly.
* @return Instance for CmdSend component
*/
public Command get_CmdSend() {
if (CmdSend == null) {
// Insert pre-init code here
CmdSend = new Command("Send", Command.OK, 1);
// Insert post-init code here
}
return CmdSend;
}
/** This method returns instance for CmdClose component and should be called instead of accessing CmdClose field directly.
* @return Instance for CmdClose component
*/
public Command get_CmdClose() {
if (CmdClose == null) {
// Insert pre-init code here
CmdClose = new Command("Close", Command.OK, 1);
// Insert post-init code here
}
return CmdClose;
}
/** This method returns instance for TxtTexto component and should be called instead of accessing TxtTexto field directly.
* @return Instance for TxtTexto component
*/
public TextField get_TxtTexto() {
if (TxtTexto == null) {
// Insert pre-init code here
TxtTexto = new TextField("Texto:", "Hello Computer.", 120, TextField.ANY);
// Insert post-init code here
}
return TxtTexto;
}
/** This method returns instance for StrItmRespuesta component and should be called instead of accessing StrItmRespuesta field directly.
* @return Instance for StrItmRespuesta component
*/
public StringItem get_StrItmRespuesta() {
if (StrItmRespuesta == null) {
// Insert pre-init code here
StrItmRespuesta = new StringItem("Respuesta:", "");
// Insert post-init code here
}
return StrItmRespuesta;
}
public void startApp() {
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}

Reply With Quote

