|
Tech Tips archive
WELCOME to the Java Developer ConnectionSM (JDC) J2ME Tech Tips, December 18, 2000. This issue covers:
This issue of the JDC J2ME Tech Tips is written by Eric Giguere (http://www.ericgiguere.com), an engineer at iAnywhere Solutions, inc, and author of the book "Java 2 Micro Edition: Professional Developer's Guide."
|
public class HelloWorld {
public static void main( String[] args ){
System.out.println( "Hello, world!" );
}
}
|
You could compile and preverify it in Windows as follows, assuming that the CLDC's bin directory is in your path:
javac -g:none -bootclasspath
%CLDC_HOME%\bin\api\classes
-d unverified HelloWorld.java
preverify -classpath %CLDC_HOME%\bin\api\classes
-d preverified unverified
|
In Solaris:
javac -g:none -bootclasspath
$CLDC_HOME/bin/api/classes
-d unverified HelloWorld.java
preverify -classpath $CLDC_HOME/bin/api/classes
-d preverified unverified
|
The HelloWorld.class in the preverified directory is the one you
actually want to run:
In Windows:
kvm -classpath
preverified;%CLDC_HOME%\bin\api\classes HelloWorld
In Solaris:
kvm -classpath
preverified:$CLDC_HOME/bin/api/classes HelloWorld
Apart from these few changes, developing in J2ME is almost the same as developing in J2SE, making the transition to Java programming for small devices relatively simple.
There is also a toolkit, called the J2ME Wireless Toolkit, that can further simplify the J2ME development process. For more information about the J2ME Wireless Toolkit, see http://java.sun.com/products/sjwtoolkit/.
For further information on the J2ME development cycle, see the white paper Java 2 Micro Edition (J2ME) Technology for Creating Mobile Devices (http://java.sun.com/products/cldc/wp/KVMwp.pdf).
For further information about the Connected Limited Device Configuration (CLDC), see http://java.sun.com/products/cldc/.
Also visit the Consumer & Embedded Technology Center (http://java.sun.com/jdc/products/j2me/) for articles, tutorials, and other resources on J2ME technologies.
HTTP connectivity is one of the requirements of the Mobile Information Device Profile (MIDP). A MIDP-enabled device must be able to interact with a web server through conventional HTTP requests. If the network doesn't directly support HTTP, then the device must route its requests through a gateway. But this is all transparent to the application developer.
In the MIDP, you interact with the network using the Generic Connection framework. (This is true for any profile built on top of the Connected Limited Device Configuration (CLDC)). The framework is a set of classes and interfaces defined by the CLDC that replaces most of the java.io and java.net classes defined by J2SE.
To make an HTTP request, use the Connector.open method
and a conventional URL, casting the result to an HttpConnection:
import javax.microedition.io.*;
HttpConnection conn = Connector.open(
"http://www.java.sun.com/jdc" );
The HttpConnection interface defines all the usual methods you
would expect to see for making HTTP requests and processing the
replies. This includes setRequestMethod, getHeaderField and
openInputStream. The HttpConnection interface makes it simple
to interact with any web site on the Internet.
One thing you will soon discover is that you won't always get the response you expect when you connect to a web site. In particular, many web sites redirect you to another URL. The Java Developer Connection web site does this, for example, when you attempt to open the page at http://www.java.sun.com/jdc -- it redirects you to http://developer.java.sun.com/developer/. Sometimes a site redirects you because the resource you are asking for has moved. Or else the site wants you to login first. If you are accessing a web site that's not under your control, you have to be prepared to handle redirections. You need to follow the redirections to obtain the data you really want.
Here's a simple class called HttpConnectionHelper that
uses the HttpConnection interface. You can use the class to
automatically follow web site redirections:
import java.io.*;
import javax.microedition.io.*;
public class HttpConnectionHelper {
public interface Callback {
void prepareRequest( String originalURL,
HttpConnection conn )
throws IOException;
}
public static HttpConnection connect( String url )
throws IOException {
return connect( url, null );
}
public static HttpConnection connect(
String url, Callback callback )
throws IOException {
HttpConnection conn = null;
String originalURL = url;
while( url != null ){
HttpConnection conn = (HttpConnection)
Connector.open( url );
if( callback != null ){
callback.prepareRequest( originalURL,
conn );
}
int rc = conn.getResponseCode();
switch( rc ){
case HttpConnection.HTTP_MOVED_PERM:
case HttpConnection.HTTP_MOVED_TEMP:
case HttpConnection.HTTP_SEE_OTHER:
case HttpConnection.HTTP_TEMP_REDIRECT:
url = conn.getHeaderField( "Location" );
if( url != null && url.startsWith(
"/*" ) ){
StringBuffer b = new StringBuffer();
b.append( "http://" );
b.append( conn.getHost() );
b.append( ':' );
b.append( conn.getPort() );
b.append( url );
url = b.toString();
}
conn.close();
break;
default:
url = null;
break;
}
}
return conn;
}
}
|
To use this class, call the connect method, passing it the
original URL:
HttpConnection conn = HttpConnectionHelper.connect(
"http://java.sun.com/jdc" );
int rc = conn.getResponseCode();
if( rc == HttpConnection.HTTP_OK ){
InputStream in = conn.openInputStream();
// do something with the response....
} else {
// unexpected response code
}
|
When invoked, the connect method connects to the original URL
and gets the HTTP response code. If the response code is the
redirection code HTTP_TEMP_REDIRECT, the connect
method gets the new URL from the Location response header and connects to that
URL. The method loops until a non-redirection response code is
returned. At that point, the method returns an HttpConnection to
the final destination.
For special cases, such as when you want to make a Post request
instead of a Get request, you can use the two-parameter form of
the connect method. Here you pass in the URL as the first
parameter, and an object implementing the
HttpConnectionHelper.Callback interface as the second parameter:
class MyCallback implements
HttpConnectionHelper.Callback {
public void prepareRequest( String originalURL,
HttpConnection conn )
throws IOException {
conn.setRequestMethod( HttpConnection.POST );
conn.setRequestProperty( "User-Agent",
"Profile/MIDP-1.0 Configuration/CLDC-1.0" );
conn.setRequestProperty(
"Content-Language", "en-US" );
}
}
HttpConnection conn = HttpConnection.connect(
"http://java.sun.com/jdc", new MyCallback() );
|
If you define a callback, it's called each time a connection is made, that is, before the response code is obtained. This lets you modify the HTTP request before it's actually made.
For further information about the Mobile Information Device Profile (MIDP), see http://java.sun.com/products/midp.
For more information about the HTTP 1.1 protocol, refer to ftp://ftp.isi.edu/in-notes/rfc2616.txt.
Note
Sun respects your online time and privacy. The Java Developer Connection mailing lists are used for internal Sun MicrosystemsTM purposes only. You have received this email because you elected to subscribe. To unsubscribe, go to the Subscriptions page (https://softwarereg.sun.com/registration/developer/en_US/subscriptions), uncheck the appropriate checkbox, and click the Update button.
Subscribe
To subscribe to a JDC newsletter mailing list, go to the Subscriptions page (https://softwarereg.sun.com/registration/developer/en_US/subscriptions), choose the newsletters you want to subscribe to, and click Update.
Feedback
Comments? Send your feedback on the JDC Tech Tips to: jdc-webmaster@sun.com
Archives
You'll find the J2ME Tech Tips archives at:
http://java.sun.com/jdc/J2METechTips/index.html
Copyright
Copyright 2000 Sun Microsystems, Inc. All rights reserved.
901 San Antonio Road, Palo Alto, California 94303 USA.
This Document is protected by copyright. For more information, see:
http://java.sun.com/jdc/copyright.html
J2ME Tech Tips
December 18, 2000
*As used in this document, the terms "Java virtual machine"
or "JVM" mean a virtual machine for the Java platform.
|
| ||||||||||||