Implementation of Hypertext Transfer Protocol on wireless J2ME devices

Source: Internet
Author: User
Tags abstract http post http request stringbuffer

As more and more mobile phones and personal digital assistants begin to integrate into the information superhighway, it becomes increasingly important to access Web sites from mobile devices. Java pioneered the small and medium storage capacity of consumer devices, the ideal language for developing mobile phones, pagers and other micro-device applications.

In this article, we'll learn how to send an HTTP GET request and an HTTP POST request to the server from a J2ME client. While this is only an exploratory article, I assume that the reader is already familiar with JAVA,J2ME and the workings of the Java Midlets (MIDP application). We will use the J2ME MIDP table and compile, configure, and test our applications using the Sun's J2ME Wireless Application Development Toolkit. For HTTP servers, any WWW address can be accessed, but by default we will use a simple Java servlet to return the details of our HTTP request.

How do I use the J2ME client to send HTTP requests to the Web server and similar servers that support HTTP? The answer is to use the J2ME network class that can be found in the Javax.microedition.io package. This article would like to elaborate on this issue.

This article outlines:

Designing a wireless network application using J2ME

. Send a hypertext GET request

. Send a hypertext POST request

. Using J2ME for wireless network programming

Java's ability to network programming is quite robust. The Java 2 Standard Edition (J2SE) defines more than 100 interface programs, classes, and exceptions in Java.io and java.net packages. The functionality that is implemented through these libraries is powerful, but it works only for traditional computer systems, which have powerful CPU processing power, fast memory and persistent data storage, but these are not realistic on most wireless devices. Therefore, J2ME defines a subset of these functions and provides a set of fixed package---javax.microedition.io packages for network and file access. Because of the wide variety of removable devices, this package defines only one set of interfaces, and leaves a real application interface implementation for each removable device vendor. This has found an optimal balance between portability and the application of device-specific features.

The abstract network and file input-output framework defined in the Javax.microedition.io class is called the Universal Connection Framework (Generic Connection Framework, referred to as GCF). GCF defines a set of abstract content to describe different methods of communication. The highest level of abstraction is called a connection (Connection), and six interfaces are declared (four are direct and two are indirect). These seven interfaces form part of the J2ME CLDC, CLDC is the configuration used by most wireless devices that can use Java. This configuration is designed to provide common network and file input and output capabilities for all CLDC devices (mobile phones, two-way pagers, low-grade PDAs, etc.). Although GCF is intended for public network and file input and output frameworks, the manufacturer does not require implementation of all interfaces declared in GCF. Some manufacturers can decide to support only socket connections, while other manufacturers can choose to support only datagram based communications. To facilitate portability across similar devices, the MIDP specification requires all MIDP devices to implement the Httpconnection interface. Httpconnection is not part of GCF, but it is derived from an interface contentconnection of GCF. We will construct our sample application using the Httpconnection interface.

Send an HTTP GET request

This section will focus on the program code, and in the next section we will only describe the common connection framework interface and the Httpconnection interface that are used to send HTTP requests and retrieve responses returned by the server. The program code to create the MIDP user interface is shown in the appendix.

Let's first define a method to put the code to send the HTTP GET request. Because some of the operations in this method have the potential to throw IOException, we will throw such an unexpected (exception) to the calling method.

public String sendHttpGet( String url ) throws IOException {
HttpConnection hcon = null;
DataInputStream dis = null;
StringBuffer message = "";
try {

The first step is to use the connector class to open a connection to the server, which is the key to GCF. We will cast this connection to the desired type, in this case the httpconnection type.

hcon = ( HttpConnection ) Connector.open( url );

Next, we get a datainputstream on the httpconnection that allows us to read the server's response data in one character at a character.

dis = new DataInputStream( hcon.openInputStream() );

Using the DataInputStream read () method, each character of the server response is lumped together into the StringBuffer object.

int ch;
while ( ( ch = dis.read() ) != -1 ) {
message = message.append( ( char ) ch );
}

Finally, the connection object is cleared to hold the resource, and the information is returned from this method.

} finally {
if ( hcon != null ) hcon.close();
if ( dis != null ) dis.close();
}//结束try/finally代码段
return message.toString();
}//结束 sendGetRequest( String )
How to send an HTTP POST request

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.