SignedMIDlet.java
/**
* Copyright (c) 2012 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia
Corporation.
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners.
* See LICENSE.TXT for license information.
*/
package com.nokia.example;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class SignedMIDlet
extends MIDlet
implements Runnable, CommandListener {
private final TextField text = new TextField("", "", 256, TextField.ANY);
private final Form form = new Form("HTTP Result");
private final Command exitCommand = new Command("Exit", Command.EXIT, 1);
private final Command okCommand = new Command("Load", Command.OK, 1);
public SignedMIDlet() {
}
public void startApp() {
if (Display.getDisplay(this).getCurrent() == null) {
form.setCommandListener(this);
}
text.setString("http://www.w3.org");
form.addCommand(exitCommand);
form.addCommand(okCommand);
form.append(text);
Display.getDisplay(this).setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void run() {
// do an action that requires permission, e.g. HTTP connection
try {
HttpConnection connection = (HttpConnection) Connector.open(text.getString());
InputStream in = connection.openInputStream();
int counter = 0;
int ch;
while ((ch = in.read()) != -1) {
counter++;
}
in.close();
connection.close();
form.append("Bytes read: " + counter+"\n");
} catch (IOException e) {
showAlert("Network error");
} catch (SecurityException e) {
showAlert("No permission to access the network");
} catch (Exception e) {
showAlert("An error occurred");
}
}
private void showAlert(String text)
{
Alert alert = new Alert("Exception");
alert.setString(text);
Display.getDisplay(this).setCurrent(alert);
}
public void commandAction(Command command, Displayable displayable) {
if (command == exitCommand) {
notifyDestroyed();
} else if (command == okCommand) {
new Thread(this).start();
}
}
}