Chapter 7 : How to network with the networks?
The world is a network. Whether it is art, science or commerce the networks are everywhere. Thus the world of mobile phones can’t be any different. In fact the mobile phones are an icon of the connected and networked world. One of the basic tasks for which the Java 2 Platform, Micro Edition was conceived was for the connectivity between the phones and the servers.
The J2ME platform enables us to communicate with the web servers very easily. Communication with the web server may be necessary due to any of the following reasons:
§ Communicating important contact data
§ Communicating financial information
§ Multi-player games
§ Personal communication eg. Instant Messaging
In fact, today the network architectures have evolved to the n-tier architectures and mobile phones and personal digital assistants are an integral part of them. Thus we see that communication is one of the most important tasks of any mobile phone based application. In this chapter we will concentrate on communication of the mobile phone with the web server. We will not get into the basics of network communication as this is beyond the scope of the book and also not very important for building practical applications. We will see the issue of network connectivity from the angle of HttpConnection interface.
The Mobile Information Device Profile 1.0 supports a sub set of the HTTP. The javax.microedition.io.HttpConnection interface is the key to our communication with the server. This interface supports the HTTP 1.1. Even though the HttpConnection interface is the best option when connecting to a web server but you may connect to web server using either the StreamConnection or the ContentConnection to retrieve the data.
The HttpConnection interface gives the methods and fields for setting and managing a HTTP connection. This interface inherits the ContentConnection interface. This is a very big interface which helps in managing all the three states of a connection, namely:
The HttpConnection interface allows the sending of requests in GET as well as POST mode. Therefore HttpConnection interface gives us all we need for a connected application.
Now we will discuss a practical example for HttpConnection interface. In this example we will set up a network connection to a particular URL. Then we will get its InputStream and read the contents which we retrieve from the web page at that particular URL.
Example Code: Retrieving data from a web server using HttpConnection
/*
* ConnectMIDlet.java
*
* Created on June 8, 2004, 09:38 PM
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.* ;
import javax.microedition.io.* ;
/**
* A MIDlet which shows a simple example for connecting a mobile phone to a web server
* and retrieving data.
* @author Saurabh Jain
* @version 1.0.0
*/
public class ConnectMIDlet extends MIDlet implements CommandListener
{
// The variable for knowing the initialization state
private boolean init ;
// Command Variables
private Command exitCommand; // The exit command
private Command Connect ;
// Important application variables
private Display display; // The display for this MIDlet
private TextBox t ;
private String st ;
public void startApp()
{
if(init == false)
{
display = Display.getDisplay(this);
Connect = new Command("Connect",Command.OK,1) ;
exitCommand = new Command("Exit", Command.EXIT, 2);
t = new TextBox("SKJ Explorer", "http://www.skjworld.com", 400, 0);
t.addCommand(Connect) ;
t.addCommand(exitCommand);
t.setCommandListener(this);
init = true ;
}
display.setCurrent(t);
}
/**
* Pause is a no-op since there are no background activities or
* record stores that need to be closed.
*/
public void pauseApp() {
}
/**
* Destroy must cleanup everything not handled by the garbage collector.
* In this case there is nothing to cleanup.
*/
public void destroyApp(boolean unconditional) {
}
/*
* Respond to commands, including exit
* On the exit command, cleanup and notify that the MIDlet has been destroyed.
*/
public void commandAction(Command c, Displayable s)
{
if(c == this.Connect)
{
t.setString(this.INet(t.getString())) ;
}
else if (c == exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
}
// Method for getting the content out of a particular URL
private String INet(String url)
{
// Initialization of important local variables
HttpConnection c = null;
InputStream is = null;
StringBuffer sb = new StringBuffer() ;
String s = "1";
try
{
// Setting up a HttpConnection
c = (HttpConnection)Connector.open(url);
c.setRequestProperty("Accept","text/html") ;
// Getting the InputStream will open the connection
is = c.openInputStream() ;
// Get the length and process the data
int len = (int)c.getLength();
if (len > 0)
{
int i,data ;
i = 0 ;
data = 0 ;
while(i < t.getMaxSize() && data != -1)
{
data = is.read() ;
sb.append((char)data) ;
i++ ;
}
s = sb.toString() ;
}
else
{
int ch;
while ((ch = is.read()) != -1)
{
sb.append(ch) ;
}
s = sb.toString() ;
}
}
catch(Exception e)
{
s = e.toString() ;
}
finally
{
try
{
if (is != null)
is.close();
if (c != null)
c.close();
}
catch(Exception e)
{
e.toString() ;
}
}
return s ;
}
}