Browse DevX
Sign up for e-mail newsletters from DevX


Sending HTTP Requests from a Wireless J2ME Device-3 : Page 3




Building the Right Environment to Support AI, Machine Learning and Deep Learning

How to Send an HTTP POST Request
As you might imagine, sending an HTTP POST request is a very similar process to sending a GET request. We'll modify one of the existing commands, add a handful of new commands, and add one additional object from the Generic Connection Framework and one additional StringBuffer object to push the content in the POST body to the server. The rest of the commands will remain unchanged.

Copy the sendHttpGet() method we just created, paste it into the same class file, and rename it to sendHttpPost(). Now we will modify this new method to send an HTTP POST request to the server.

Add two new variable declarations to the top of the method. Declare a variable of type DataOutputStream and declare another variable of type String. We will use the DataOutputStream object to push our POST request body (contained in the String) to the server.

DataOutputStream dos = null; 

String requestBody = null; 

Modify the Connector.open() command to include another argument, indicating that the connection should allow the client to both read and write data to the server over the connection.

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

Set the request method used by the HttpConnection object to POST (the method is GET by default).

hcon.setRequestMethod( HttpConnection.POST ); 

Obtain a DataOutputStream object for the existing HTTP connection. 

dos = hc.openDataOutputStream();  

Declare an array of bytes and initialize that array by retrieving a byte array from the requestBody string. Then write the contents of the byte array to the buffer of the DataOutputStream.

byte[] byteRequest = requestBody.getBytes(); 

for( int i = 0; i < byteRequest.length; i++ ) { 


}//end for( int i = 0; i < byteRequest.length; i++ )

The next line is a bit controversial.

dos.flush(); //Including this line may produce undesiredresults on certain devices

If you call c.flush(), some devices will send the request in HTTP 1.1 "chunked" encoding by adding a "Transfer-encoding: chunked" header to the request and writing the size of each block of data in hexadecimal form as a text line before the data bytes themselves. This is repeated for any number of blocks you send, usually demarcated by calls to flush().

If you have a HTTP 1.1-compliant Web server this should be transparent to your server side scripts, but if you do not, you will either want to avoid calling flush() (though some devices may still chunk for you automatically) or write some HTTP 1.1 chunk handling code in your server scripts. All you will need to do in the latter case is read the first text line of the response, extract the size from it (i.e. "C\r\n" where C means 12 bytes of chunk follow), and then read 12 bytes of response data. Then read another line to see if there is another chunk size, and repeat until stream is closed or your script has all the data it needs. See the HTTP 1.1 specification for more details.

Editor's Note: Thanks to DevX reader Marc Palmer for technical assistance.

The rest of the method remains the same, except that the DataOutputStream object must be closed inside the finally{} block.

} finally { 

    if ( hc != null ) hc.close();
    if ( dis != null ) dis.close();
    if ( dos != null ) dis.close();
}//end try/finally 

That's all there is to it! Remember that you can obtain the full source code for this HTTP-enabled MIDP application by clicking on the appropriate link at the end of this article (the bottom of the page).

As Internet-ready and network-enabled wireless devices continue to grow in popularity, the importance of Java and the Java 2 Micro Edition will also continue to grow. Since the HTTP protocol is the only current networking protocol that is supported by all MIDP-compliant devices, it is the best candidate for developing wireless networked applications.

In this article we have explored the basic architecture and a few of the core issues associated with wireless network programming, and we've seen how to invoke the two most common HTTP request methods, GET and POST. J2ME is still in its infancy, and wireless devices are very close to a "tipping point" of popularity. The next few months and years should be very interesting.

DevX Java Pro Kyle Gabhart is a consultant for Objective Solutions, a high-end engineering services company focusing on the medical, semiconductor, and telecommunications domains. He is an Associate Member of the Worldwide Institute of Software Architects (WWISA), an organization dedicated to promoting and enriching software architecture as a profession. Kyle is a co-author of "Professional Java and XML".
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date