How to make HTTP connection in J2ME

One of the most critical aspects of J2ME is network connectivity. Although J2ME devices can be useful when they are not connected to a network, the ability to make a network connection provides a means to tap into the powerful resources available on a network. In other words, a client-server paradigm is used to offload complicated work from the limited capabilities of the MIDP device to the more capable server environment.

Since network connectivity is so vital to J2ME it is important that the architecture be extendible to many different protocols while allowing applications to be portable across many devices. (MIDP 1.0 specification supports only HTTP connection) The piece of software within the J2ME architecture that addresses network connectivity is called the Generic Connection Framework (GCF). The Generic Connection Framework resides in the javax.microedition.io

  • DatagramConnection : Manages a datagram connection.
  • InputConnection : Manages an input stream-based connection.
  • OutputConnection : Manages an output stream-based connection.
  • StreamConnection : Manages the capabilities of a stream. Combines the methods of both InputConnection and OutputConnection.
  • ContentConnection :Manages connection for passing content, such as HTML or XML. Provides basic methods for inspecting the content length, encoding and type of content.

Let’s talk about more specific sample, below code snippet demonstrates simple HTTP connection. First crucial point in snippet, connection type casting. How the Connector knows the right connection class? Answer is very forthright; when the URI is defined in open, Connector parses it (schema, address and parameters) and decides the class. For instance “http” token refers to HttpConnection class.

   1: String url = new String("http://sample.cs.hacettepe.edu.tr:1234/pusu/pusunotifier");
   2: try {
   3:     con = (HttpConnection) Connector.open(url);
   4:     con.setRequestMethod(HttpConnection.POST);
   5:     out = con.openOutputStream();
   6:     out.write(stringData.getBytes());
   7: } catch (IOException exp) {
   8:     // Handle IOException
   9: }
  10: // Final snippet 

Building HTTP header is another vital aspect of the connection. For MIDlet-to-Servlet connections, that’s quite simple, all you needed defining HttpConnection.POST in seRequestMethod. What about MIDlet-to-PHP interaction? That’s offtopic for this tutorial but I can briefly say that you have to build HTTP header manually. This method covers setting Boundary, Content-Disposition, Content-Type, etc… (That's valid for hybrid transmission. Only string based data transmission can be handled without manual building).

For client side final point is closing connection resources to avoid lack:

   1: // Connection snippet
   2: finally {
   3:     try {
   4:         if (out != null) {
   5:             out.close();
   6:         }
   7:         if (con != null) {
   8:             con.close();
   9:         }
  10:     } catch (IOException exp) {
  11:         // Handle IOException
  12:     }
  13: }

That’s all for the client side application. Let’s review implemented part of the application; client constructs sending data, requests HTTP connection and transmits concerning data. Server side requirements; establishing HTTP connection, receiving transmitted data and process it.

   1: public class PusuNotifier extends HttpServlet {
   2:     public void doPost(HttpServletRequest request, HttpServletResponse response) {
   3:         InputStream input = request.getInputStream();
   4:         BufferedReader bufReader = new BufferedReader(new InputStreamReader(input));
   5:         StringBuffer stringBuf = new StringBuffer();
   6:         String line;
   7:         while ((line = bufReader.readLine())!=null) {
   8:             stringBuf.append(line);
   9:         }
  10:         String receivedData = stringBuf.toString();
  11:         // Process received data
  12:     }
  13: }

Establishing connection is provided by container (server) application for Servlets so this requirement has been skipped. Standart HttpServlet specification defines doPost method in order to handle post requests and HttpServletRequest argument to encapsulate client’s request.

Receiving data via BufferedReader is a generic way to handle transmission. (Although it’s suitable way for hybrid transmission, not the only way). Finally, received data is server side and ready to be processed.

Originally posted by Orçun Dayıbaş in 2005.04.25 - The Pusu Project Blog

0 comments: