Network connection part 2: Read from the server
Written by: maffelu , 2009-07-20 13:22:54
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/** * An applet to read files from a network * @author maffelu */
public class ReadTextFile extends MIDlet implements CommandListener{
//======================FIELDS===================
private String url = "http://localhost:8080/WebApplication1/java.txt";
private Command cmdExit;
private Command cmdRead;
private TextBox textBox;
//=====================PROPERTIES================
/** * Returns an exit command * @return A Command object */
public Command getCmdExit(){
if(cmdExit == null){
cmdExit = new Command("Exit", Command.EXIT, 0);
}
return cmdExit;
}
/** * Returns a read command * @return A Command object */
public Command getCmdRead(){
if(cmdRead == null){
cmdRead = new Command("Read", Command.OK, 1);
}
return cmdRead;
}
/** * Returns a textbox * @return A TextBox object */
public TextBox getTextBox(){
if(textBox == null){
textBox = new TextBox("TextFileTest", "Click to fetch textfile...", 255, TextField.ANY);
textBox.addCommand(getCmdExit());
textBox.addCommand(getCmdRead());
textBox.setCommandListener(this);
}
return textBox;
}
//====================METHODS==========================
public Display getDisplay(){
return Display.getDisplay(this);
}
/** * Switches the current display * @param alert An alert object, can be NULL * @param displayable The displayable object to switch to */
public void switchDisplay(Alert alert, Displayable displayable){
Display display = getDisplay();
if(alert == null){
display.setCurrent(displayable);
} else {
display.setCurrent(alert, displayable);
}
}
public void startMIDlet(){
switchDisplay(null, getTextBox());
}
public void exitMIDlet(){
switchDisplay(null, null);
destroyApp(true);
notifyDestroyed();
}
/** * Reads the textfile and outputs it in the TextBox * @param urlString The address to the file to read */
private void readTextFile(String urlString){
StreamConnection conn = null;
InputStream in = null;
StringBuffer data = new StringBuffer();
try{
conn = (StreamConnection)Connector.open(urlString);
in = conn.openInputStream();
int ch;
while((ch = in.read()) != -1){
data.append((char)ch);
}
} catch (IOException ioe){
System.err.println(ioe.getMessage());
} finally {
try{
if(in != null){
in.close();
}
if(conn != null){
conn.close();
}
} catch (IOException ioe){
System.err.println(ioe.getMessage());
}
}
textBox.setString(data.toString());
switchDisplay(null, textBox);
}
public void commandAction(Command command, Displayable displayable){
if(displayable == textBox){
if(command == cmdExit){
exitMIDlet();
} else if(command == cmdRead){
Form waitForm = new Form("Wait");
switchDisplay(null, waitForm);
Thread t = new Thread(){
public void run(){
readTextFile(url);
}
};
t.start();
}
}
}
public void startApp() {
startMIDlet();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
conn = (StreamConnection)Connector.open(urlString);
in = conn.openInputStream();
int ch;
while((ch = in.read()) != -1){
data.append((char)ch);
}
There are no comments on this article.
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.