You can easily remove the reference to the MIDlet, by statically calling the fetch method. You can also place the fetch method in a separate class if you like. For example you can do this:
MultipleConnections MIDlet:
Code:
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.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class MultipleConnections extends MIDlet implements CommandListener{
static Form mainform;
Command infoCommand=new Command("Info",Command.OK,1);
Command exitCommand=new Command("Exit",Command.EXIT,0);
connection con1;
connection con2;
connection con3;
static boolean istaken;
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException {}
protected void pauseApp() { }
protected void startApp() throws MIDletStateChangeException {
istaken=false;
mainform=new Form("Multi Connections");
mainform.addCommand(infoCommand);
mainform.addCommand(exitCommand);
con1=new connection("http://www.w3schools.com/html/default.asp");
con2=new connection("http://www.w3schools.com/html/html_fonts.asp");
con3=new connection("http://www.w3schools.com/html/html_xhtml.asp");
mainform.setCommandListener(this);
Display.getDisplay(this).setCurrent(mainform);
}
public static boolean isTaken()
{
return istaken;
}
public Form getForm()
{
return mainform;
}
public void commandAction(Command arg0, Displayable arg1) {
if(arg0==exitCommand)
{
notifyDestroyed();
}
if(arg0==infoCommand)
{
mainform.append("\nMIDlet is responsive\n");
}
}
}
connection class:
Code:
public class connection implements Runnable{
static boolean istaken=false;
Thread t;
String targetUrl;
connection(String url)
{
targetUrl=url;
t=new Thread(this);
t.start();
}
public void run() {
while(MultipleConnections.isTaken())
{
try
{
Thread.sleep(2000);
}
catch(Exception e){}
}
connect.fetch(targetUrl);
}
}
connect class:
Code:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
public class connect {
public static void fetch(String targetUrl)
{
MultipleConnections.istaken=true;
MultipleConnections.mainform.append("\nRetrieving page...\n");
String response="";
String substr="";
HttpConnection connection = null;
InputStream inputstream = null;
try
{
connection = (HttpConnection) Connector.open(targetUrl);
//HTTP Request
connection.setRequestMethod(HttpConnection.GET);
// HTTP Response
if (connection.getResponseCode() == HttpConnection.HTTP_OK)
{
inputstream = connection.openInputStream();
int length = (int) connection.getLength();
if (length != -1)
{
byte incomingData[] = new byte[length];
inputstream.read(incomingData);
response = new String(incomingData);
}
else
{
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int ch;
while ((ch = inputstream.read()) != -1)
{
bytestream.write(ch);
}
response = new String(bytestream.toByteArray());
bytestream.close();
}
}
}
catch(IOException error)
{
System.out.println("Caught IOException: " + error.toString());
}
finally
{
if (inputstream!= null)
{
try
{
inputstream.close();
}
catch( Exception error)
{
/*log error*/
}
}
if (connection != null)
{
try
{
connection.close();
}
catch( Exception error)
{
/*log error*/
}
}
}
if(response.indexOf("<title>")>=1 && response.indexOf("</title>")>=1)
{
int start=response.indexOf("<title>");
int end=response.indexOf("</title>");
substr=response.substring(start+7,end);
MultipleConnections.mainform.append("The title of the page is: "+substr);
}
else
{
MultipleConnections.mainform.append("The title of the page could not be retrieved");
}
MultipleConnections.mainform.append("\nDone!\n\n");
MultipleConnections.istaken=false;
}
}