How to realize HTTP connections using J2ME
Author : Jbuenol
From TechnologicalWiki
Contents |
[edit] Introduction
The J2ME Enviroment allows to mobile devices HTTP connections with servers to require any service. The target of this article is to be a tutorial to beginners which they will can implement a communicatione between the peers. You can check how to prepare the enviroment doing click over here.
[edit] Protocol
The first step is the design of the protocol to communicate both parts. The most easy way is to transfer plain text form the server to the device :
1. An URL will be used by the device application to make a request :
e.g.
http://myserver.com/Appointments.jsp?IDUser=Bueno&Date=Today
IDUser an Date are the variables which contain the values to send to the server.
2. The server send the response in plain text :
e.g.
Appointments for today:
9:00-14:00 J2ME Course.
20:00-21:00 Go to the swimming Pool.
This response is treated by the client ( mobile device ) as we like.
[edit] The J2ME Client
We are going to learn how to implement a HTTP Client creating a MIDlet in JAVA.
[edit] The Connection
To achieve the connection we are going to use the HttpConnection object with which we connect the client with the server to do the request.
HttpConnection connection = (HttpConnection) Connector.open("http://myserver.com/Page.jsp");
It's very important to do the connection process in other thread to avoid the execution to block.
Thread t = new Thread ( this ); t.start();
When the start method is called the thread is thrown and the connection is not blocked.
[edit] The Comunication
[edit] ReadData (Receive from the server)
The object used to read data from the server is the InputStream object:
public InputStream input
This object represents the flow data from the server. When the connection has been opened, this flow must be assigned to the Input Stream:
input = connection.openInputStream();
long = (int)connection.getLength();
The method read is used to read the data from the buffer. This data is in bytes format:
byte[] responseBytes;
...
responseBytes = new byte[long];
input.read( responseBytes );
...
String response = new String(responseBytes ,0, long);
Another way to obtain the string is using the DataInputStream object. It use the readUTF method to get a string :
DataInputStream in = new DataInputStream(input);
String response = "";
response = in.readUTF();
[edit] WriteData (Send to the server)
The object used to write data to send them to the server is the OutputStream object. It's is used like the InputStream :
public OutputStream input
This object represents the data flow from the server. When the connection has been opened, this flow must be assigned to the Input Stream:
output = connection.openOutputStream();
The method write is used to write the data in the buffer to send them to the server :
byte[] responseBytes;
...
output.write( responseBytes );
output.flush();
...
[edit] HelloMidlet
HelloMIDlet.java is the typical example of the "helloworld" application, that we can use for a first time test. Here is the code.
[edit] The J2ME Server
Now, we know how to create a J2ME client. The client applications are sent to the HTTP server which processes the request, generates the response and returns back it to the client. The server can be written on several languages :
A complete example of how to create a HTTP Server ( HelloWorld using Servlets ).
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class MyServerServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String message = "Hello World !!!";
response.setContentType("text/plain");
response.setContentLength(message.length());
/* getWriter() obtains the output flow to the client */
PrinterWriter out = response.getWriter();
/* send the response */
out.println(message);
}
}
[edit] Also see
[edit] References
Jonathan Knudsen, "Wireless Java: Developing with J2ME", 2nd edition. Ed APress, 2003
James Keohgh, "The Complete Reference: J2ME", Ed. Osborne,2003


