Programming Tutorials

The MIDP Networking Model in J2ME

By: Emiley J in J2ME Tutorials on 2007-09-17  

The MIDP networking model in J2ME provides a set of classes and interfaces to enable mobile devices to communicate over various network protocols such as HTTP, TCP, and UDP.

Here is an example that demonstrates how to use the MIDP networking model to download a file from a remote server using HTTP:


public class HttpDownloader {

    private String url;

    public HttpDownloader(String url) {
        this.url = url;

    public void download() throws IOException {
        HttpConnection connection = null;
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            // Open the connection to the URL
            connection = (HttpConnection);

            // Set the request method to GET

            // Check the response code to ensure that the connection is successful
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpConnection.HTTP_OK) {
                // Get the input and output streams
                inputStream = connection.openInputStream();
                outputStream = new ByteArrayOutputStream();

                // Read the data from the input stream and write it to the output stream
                byte[] buffer = new byte[1024];
                int bytesRead = -1;
                while ((bytesRead = != -1) {
                    outputStream.write(buffer, 0, bytesRead);

                // Display the downloaded content
            } else {
                throw new IOException("HTTP error code: " + responseCode);
        } finally {
            // Close the input and output streams and the connection
            if (inputStream != null) {
            if (outputStream != null) {
            if (connection != null) {

    public static void main(String[] args) {
        try {
            HttpDownloader downloader = new HttpDownloader("");
        } catch (IOException e) {


In this example, the HttpDownloader class takes a URL as input and downloads the content of the URL using an HttpConnection. The getResponseCode() method is used to check the HTTP response code to ensure that the connection is successful. The input stream is read and the output stream is written to display the downloaded content.

This is just one example of how to use the MIDP networking model to communicate over HTTP. There are other classes and interfaces available in the MIDP networking model that can be used to communicate over other network protocols such as TCP and UDP.

Add Comment

* Required information


No comments yet. Be the first!

Most Viewed Articles (in J2ME )

Latest Articles (in J2ME)