6.3. HTTP Communication

6.3.1. Dependencies

HTTP communication requires the NET foundation library and the HTTP Client add-on library. To use them, add the following dependencies to your project’s module.ivy file:

<dependency org="ej.api" name="net" rev="1.1.2" />
<dependency org="ej.library.eclasspath" name="httpclient" rev="1.5.0" />

6.3.2. Replacing the javax.microedition.io.HttpConnection class

Code using javax.microedition.io.HttpConnection can use sun.net.www.protocol.http.HttpURLConnection instead.

6.3.2.1. Code sample

The following sample is extracted from the J2ME Javadoc.

void getViaHttpConnection(String url) throws IOException {
    HttpConnection c = null;
    InputStream is = null;
    int rc;

    try {
        c = (HttpConnection)Connector.open(url);

        // Getting the response code will open the connection,
        // send the request, and read the HTTP response headers.
        // The headers are stored until requested.
        rc = c.getResponseCode();
        if (rc != HttpConnection.HTTP_OK) {
            throw new IOException("HTTP response code: " + rc);
        }

        is = c.openInputStream();

        // Get the ContentType
        String type = c.getType();

        // Get the length and process the data
        int len = (int)c.getLength();
        if (len > 0) {
            int actual = 0;
            int bytesread = 0 ;
            byte[] data = new byte[len];
            while ((bytesread != len) && (actual != -1)) {
                actual = is.read(data, bytesread, len - bytesread);
                bytesread += actual;
            }
        } else {
            int ch;
            while ((ch = is.read()) != -1) {
                ...
            }
        }
    } catch (ClassCastException e) {
        throw new IllegalArgumentException("Not an HTTP URL");
    } finally {
        if (is != null)
            is.close();
        if (c != null)
            c.close();
    }
 }

It can be adapted as follows:

void getViaHttpConnection(String url) throws IOException {
    HttpURLConnection c = null;
    InputStream is = null;
    int rc;

    try {
        c = new HttpURLConnection(new URL(url));

        // Getting the response code will open the connection,
        // send the request, and read the HTTP response headers.
        // The headers are stored until requested.
        rc = c.getResponseCode();
        if (rc != HttpURLConnection.HTTP_OK) {
            throw new IOException("HTTP response code: " + rc);
        }

        is = c.getInputStream();

        // Get the ContentType
        String type = c.getContentType();

        // Get the length and process the data
        int len = c.getContentLength();
        if (len > 0) {
            int actual = 0;
            int bytesread = 0;
            byte[] data = new byte[len];
            while ((bytesread != len) && (actual != -1)) {
                actual = is.read(data, bytesread, len - bytesread);
                bytesread += actual;
            }
        } else {
            int ch;
            while ((ch = is.read()) != -1) {
                ...
            }
        }
    } catch (ClassCastException e) {
        throw new IllegalArgumentException("Not an HTTP URL");
    } finally {
        if (is != null) {
            is.close();
        }
        if (c != null) {
            c.disconnect();
        }
    }
}

6.3.2.2. HTTP Status Codes

HTTP Status Codes have the exact same names.

For instance, HttpConnection.HTTP_ACCEPTED corresponds to HttpURLConnection.HTTP_ACCEPTED, and HttpConnection.HTTP_FORBIDDEN corresponds to HttpURLConnection.HTTP_FORBIDDEN.

Warning

The following HTTP Status Codes are not available as static String in java.net.HttpURLConnection:

  • HTTP_EXPECT_FAILED (417)
  • HTTP_TEMP_REDIRECT (307)
  • HTTP_UNSUPPORTED_RANGE (416)

6.3.2.3. HTTP methods

setRequestMethod() and getRequestMethod() support more HTTP methods than their J2ME counterparts.

In addition to GET, POST and HEAD, the following methods are now supported:

  • OPTIONS
  • PUT
  • DELETE
  • TRACE

Note

HTTP method names are not provided as static String, so they need to be explicitly written when calling setRequestMethod() or checking the result of getRequestMethod().

6.3.3. API

The API documentation of HttpURLConnection is available here.