import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
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.StringItem;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class SampleClient extends MIDlet implements CommandListener {
private static String url = "http://localhost:8080/servlet/j2me.techtips.SampleServer";
private Display display;
private Command exitCommand = new Command("Exit",Command.EXIT,1);
private Command okCommand = new Command("OK",Command.EXIT,1);
private Command sendCommand = new Command("Send",Command.EXIT,1);
private TextBox entryForm;
public SampleClient(){
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
exitMIDlet();
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
if(display == null){
initMIDlet();
}
}
private void initMIDlet(){
display = Display.getDisplay(this);
entryForm = new EntryForm();
display.setCurrent(entryForm);
}
private void exitMIDlet(){
notifyDestroyed();
}
public void commandAction(Command c, Displayable d) {
if(c == sendCommand){
StatusForm f = new StatusForm(entryForm.getString());
display.setCurrent(f);
f.start();
}else if(c == okCommand){
display.setCurrent(entryForm);
}else{
exitMIDlet();
}
}
class EntryForm extends TextBox {
EntryForm(){
super("Enter some text","",80,0);
addCommand(exitCommand);
addCommand(sendCommand);
setCommandListener(SampleClient.this);
}
}
class StatusForm extends Form implements Runnable ,HttpConnectionHelper.Callback {
private StringItem message;
private byte[] data;
StatusForm (String text){
super("Statue");
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
dout.writeUTF(text);
data = bout.toByteArray();
dout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
void display(String text){
if(message == null){
message = new StringItem(null,text);
append(message);
}else{
message.setText(text);
}
}
void done(String msg){
display(msg!=null?msg:"Done.");
addCommand(okCommand);
setCommandListener(SampleClient.this);
}
public void prepareRequest(String originalURL,HttpConnection conn ) throws IOException{
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
conn.setRequestProperty("Content-Language", "en-US");
conn.setRequestProperty("Accept", "application/octet-stream");
conn.setRequestProperty("Connection", "close");
conn.setRequestProperty("Content-Length", Integer.toString(data.length));
OutputStream os = conn.openOutputStream();
os.write(data);
os.close();
}
public void run(){
HttpConnection conn = null;
display("Obtaining HttpConnection object...");
try {
conn = HttpConnectionHelper.connect(url,this);
display("Connecting to the server...");
int rc = conn.getResponseCode();
if(rc == HttpConnection.HTTP_OK){
StringBuffer text = new StringBuffer();
try {
DataInputStream din = new DataInputStream(conn.openInputStream());
int n = din.readInt();
while(n-->0){
text.append(din.readUTF());
text.append('\n');
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
void start(){
display("Starting...");
Thread t = new Thread(this);
try {
t.start();
} catch (Exception e) {
done("Exception " + e + " trying to start thread.");
}
}
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
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.StringItem;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class SampleClient extends MIDlet implements CommandListener {
private static String url = "http://localhost:8080/servlet/j2me.techtips.SampleServer";
private Display display;
private Command exitCommand = new Command("Exit",Command.EXIT,1);
private Command okCommand = new Command("OK",Command.EXIT,1);
private Command sendCommand = new Command("Send",Command.EXIT,1);
private TextBox entryForm;
public SampleClient(){
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
exitMIDlet();
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
if(display == null){
initMIDlet();
}
}
private void initMIDlet(){
display = Display.getDisplay(this);
entryForm = new EntryForm();
display.setCurrent(entryForm);
}
private void exitMIDlet(){
notifyDestroyed();
}
public void commandAction(Command c, Displayable d) {
if(c == sendCommand){
StatusForm f = new StatusForm(entryForm.getString());
display.setCurrent(f);
f.start();
}else if(c == okCommand){
display.setCurrent(entryForm);
}else{
exitMIDlet();
}
}
class EntryForm extends TextBox {
EntryForm(){
super("Enter some text","",80,0);
addCommand(exitCommand);
addCommand(sendCommand);
setCommandListener(SampleClient.this);
}
}
class StatusForm extends Form implements Runnable ,HttpConnectionHelper.Callback {
private StringItem message;
private byte[] data;
StatusForm (String text){
super("Statue");
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
dout.writeUTF(text);
data = bout.toByteArray();
dout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
void display(String text){
if(message == null){
message = new StringItem(null,text);
append(message);
}else{
message.setText(text);
}
}
void done(String msg){
display(msg!=null?msg:"Done.");
addCommand(okCommand);
setCommandListener(SampleClient.this);
}
public void prepareRequest(String originalURL,HttpConnection conn ) throws IOException{
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
conn.setRequestProperty("Content-Language", "en-US");
conn.setRequestProperty("Accept", "application/octet-stream");
conn.setRequestProperty("Connection", "close");
conn.setRequestProperty("Content-Length", Integer.toString(data.length));
OutputStream os = conn.openOutputStream();
os.write(data);
os.close();
}
public void run(){
HttpConnection conn = null;
display("Obtaining HttpConnection object...");
try {
conn = HttpConnectionHelper.connect(url,this);
display("Connecting to the server...");
int rc = conn.getResponseCode();
if(rc == HttpConnection.HTTP_OK){
StringBuffer text = new StringBuffer();
try {
DataInputStream din = new DataInputStream(conn.openInputStream());
int n = din.readInt();
while(n-->0){
text.append(din.readUTF());
text.append('\n');
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
void start(){
display("Starting...");
Thread t = new Thread(this);
try {
t.start();
} catch (Exception e) {
done("Exception " + e + " trying to start thread.");
}
}
}
}