Oi,
Comecei agora a desenvolver aplicações em J2ME.
Para começar estou a tentar implementar um servidor de http.
Já coloquei a correr o programa no emulador e está a funcionar. O pior é quando coloco no PDA.
No PDA dá o erra ConnectionNotFoundException quando tento correr o MIDlet.
Alguem sabe o que poderá estar mal?
package hello;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Reader;
import javax.microedition.io.Connector;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.io.SocketConnection;
import javax.microedition.midlet.<em>;
import javax.microedition.lcdui.</em>;
/**
*
*/
public class HelloMidlet extends MIDlet implements CommandListener,Runnable {
private ServerSocketConnection mServerSocketConnection;
private boolean mTrucking = true;
/** Creates a new instance of HelloMidlet */
public HelloMidlet() {
}
private Form helloForm;
private StringItem helloStringItem;
private Command exitCommand;
/** This method initializes UI of the application.
*/
private void initialize() {
Thread t = new Thread(this);
t.start();
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
}
}
// 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_helloStringItem()});
helloForm.addCommand(get_exitCommand());
helloForm.setCommandListener(this);
// Insert post-init code here
}
return helloForm;
}
/** This method returns instance for helloStringItem component and should be called instead of accessing helloStringItem field directly.
* @return Instance for helloStringItem component
*/
public StringItem get_helloStringItem() {
if (helloStringItem == null) {
// Insert pre-init code here
helloStringItem = new StringItem("Servidor J2ME", "Teste Servidor!");
// Insert post-init code here
}
return helloStringItem;
}
/** 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;
}
public void startApp() {
initialize();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
private void log(String text) { log(null, text); }
private void log(String label, String text) {
StringItem si = new StringItem(label, text);
si.setLayout(Item.LAYOUT_NEWLINE_AFTER);
helloForm.append(si);
}
public void run() {
try {
mServerSocketConnection = (ServerSocketConnection)
Connector.open("socket://:80");
log("Startup complete.");
SocketConnection sc = null;
while (mTrucking) {
sc = (SocketConnection)
mServerSocketConnection.acceptAndOpen();
log("client: ", sc.getAddress());
// Strictly speaking, each client connection
// should be handled in its own thread. For
// simplicity, this implementation handles
// client connections inline.
Reader in = new InputStreamReader(
sc.openInputStream());
String line;
while ((line = readLine(in)) != null) ;
// Ignoring the request, send a response.
PrintStream out = new PrintStream(sc.openOutputStream());
out.print("HTTP/1.1 200 OK\r\n\r\n");
out.print(getMessage());
out.close();
in.close();
sc.close();
}
}
catch (Exception e) {
log("exception: ", e.toString());
}
}
private String readLine(Reader in) throws IOException {
// This is not efficient.
StringBuffer line = new StringBuffer();
int i;
while ((i = in.read()) != -1) {
char c = (char)i;
if (c == ‘\n’) break;
if (c == ‘\r’) ;
else line.append©;
}
if (line.length() == 0) return null;
return line.toString();
}
private java.util.Random mRandom = new java.util.Random();
private String getMessage() {
int i = Math.abs(mRandom.nextInt()) % 5;
String s = null;
switch (i) {
case 0: s = “Above all the others we’ll fly”; break;
case 1: s = “There is no reason to hide”; break;
case 2: s = “I dreamed about Ray Charles last night”; break;
case 3: s = “Someone keeps moving my chair”; break;
case 4: s = “Joseph’s face was black as night”; break;
default: break;
}
return s;
}
}