dcsimg
How can I send data from J2ME program?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Aitor_Zabaleta
Posted On:   Thursday, November 14, 2002 12:07 AM

Hi. I´m making a client/server application. The client will be in my Palm with J2ME, and the server, will be Java program in my laptop.



How can I send data from the J2ME program to the server?



Please, if you can, put here what function must I use or some sample code.



Tank you very much.

Re: How can I send data from J2ME program?

Posted By:   Bjornie_Wikstroem  
Posted On:   Friday, November 15, 2002 01:45 PM

Here's a sample class:


import java.io.*;
import javax.microedition.io.*;

public class CMail {
private StreamConnection streamConnection;
private InputStream inputStream;
private OutputStream outputStream;
private String[] mailParameters;

public CMail( String subject, String message, String from, String to ) {
String smtpServer = "";
try {
smtpServer = "127.0.0.1";
}
catch( Exception e ) {
// error
}

try {
this.streamConnection = ( StreamConnection ) Connector.open(
"socket://" + smtpServer + ":25", Connector.READ_WRITE
);
}
catch( Exception e ) {
// error
}

this.mailParameters = new String[ 5 ];
this.mailParameters[ 0 ] = "helo " + smtpServer;
this.mailParameters[ 1 ] = "MAIL FROM:<" + from + ">";
this.mailParameters[ 2 ] = "RCPT TO:<" + to + ">";
this.mailParameters[ 3 ] = "DATA";
this.mailParameters[ 4 ] = "Subject: " + subject + "

" + message + "
.";
}

public void start() {
try {
this.inputStream = this.streamConnection.openDataInputStream();
this.outputStream = this.streamConnection.openOutputStream();

for( int i = 0; i < this.mailParameters.length; i++ ) {
send( this.mailParameters[ i ] );
recive();
}

this.inputStream.close();
this.outputStream.close();
this.streamConnection.close();
}
catch( Exception e ) {
// error
}
}

private void send( String sendMe ) throws Exception {
sendMe.concat( "
" );

byte[] b = sendMe.getBytes();
this.outputStream.write( b );
this.outputStream.flush();
}

private void recive() throws Exception {
StringBuffer stringBuffer = new StringBuffer();

int character;
while( ( character = this.inputStream.read() ) != -1 )
stringBuffer.append( ( char ) character );
}
}



Good luck!


Regards,

Bjornie

Re: How can I send data from J2ME program?

Posted By:   Lasse_Koskela  
Posted On:   Thursday, November 14, 2002 04:23 AM

Check out the API for javax.microedition.io. Also, vendor specific APIs could be of help.
About | Sitemap | Contact