il codice seguente mi esegue il caricamento di un'immagine sul display del cellulare, vorrei che tale immaggine sia caricata ripetutamente,in quanto in realtà l'immagne photo.jpg varia nel tempo, ho provato ad inserire un while infinito nel metodo run ma non funziona. qualcuno ha qualche idea???
codice:
package client.mobile;
import java.io.DataInputStream;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import javax.microedition.lcdui.game.Sprite;
public class Client implements Runnable, CommandListener {
private SocketMIDlet parent;
private Display display;
private Form f;
private StringItem si;
private TextField tf;
private boolean stop;
private Command sendCommand = new Command("Send", Command.ITEM, 1);
InputStream is;
Sender sender;
public Client(SocketMIDlet m) {
parent = m;
display = Display.getDisplay(parent);
f = new Form("Picture");
display.setCurrent(f);
}
/**
* Avvio del thread client
*/
public void start() {
Thread t = new Thread(this);
t.start();
}
public void commandAction(Command c, Displayable s) {
if (c == sendCommand && !parent.isPaused()) {
sender.send(tf.getString());
}
if (c == Alert.DISMISS_COMMAND) {
parent.notifyDestroyed();
parent.destroyApp(true);
}
}
/**
* Chiusura di tutti gli streams aperti
*/
public void stop() {
try {
stop = true;
sender.stop();
if (is != null) {
is.close();
}
} catch (Exception ioe) {
System.out.println("Err3");
ioe.printStackTrace();
}
}
public void run(){
try {
getViaHttpConnection("http://localhost:88/photo.jpg");
} catch (IOException ex) {
System.out.println("Err4");
ex.printStackTrace();
}
}
/**
* Connessione e caricamento immaggine sul dispaly
*/
void getViaHttpConnection(String url) throws IOException {
HttpConnection c = null;
InputStream is = null;
int rc;
Image img = null;
try {
c = (HttpConnection)Connector.open(url);
rc = c.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
throw new IOException("HTTP response code: " + rc);
}
is = c.openInputStream();
String type = c.getType();
int len = (int)c.getLength();
System.out.println(len);
if (len > 0) {
int actual = 0;
int bytesread = 0 ;
byte[] data = new byte[len];
while ((bytesread != len) && (actual != -1)) {
actual = is.read(data, bytesread, len - bytesread);
bytesread += actual;
}
img = Image.createImage(data, 0, len);
img = resizeImage(img);
f.append(img);
}
} catch (ClassCastException e) {
throw new IllegalArgumentException("Not an HTTP URL");
} finally {
if (is != null)
is.close();
if (c != null)
c.close();
}
}