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:
import java.io.*; import javax.microedition.io.*; 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) Connector.open(url); // Set the request method to GET connection.setRequestMethod(HttpConnection.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 = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } // Display the downloaded content System.out.println(outputStream.toString()); } else { throw new IOException("HTTP error code: " + responseCode); } } finally { // Close the input and output streams and the connection if (inputStream != null) { inputStream.close(); } if (outputStream != null) { outputStream.close(); } if (connection != null) { connection.close(); } } } public static void main(String[] args) { try { HttpDownloader downloader = new HttpDownloader("http://www.example.com/file.txt"); downloader.download(); } catch (IOException e) { e.printStackTrace(); } } }
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
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
GUI components and menu based J2ME Applications.
Code sample to Send SMS from a J2ME application.
Adding your own Application icon for your J2ME application (jar file)
Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
Datagrams in J2ME (UDP Programming sample)
Client Server in J2ME (Socket Programming sample)
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
Using HTTP vs UDP vs Socket in J2ME
RMSCookieConnector - Using Cookies in J2ME
POST UTF-8 encoded data to the server in J2ME
Using alerts and tickers in J2ME
lists, forms, choices, gauges, text fields, text boxes in J2ME
Comments