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.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
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:
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.
Most Viewed Articles (in J2ME )
Adding your own Application icon for your J2ME application (jar file)
Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
Client Server in J2ME (Socket Programming sample)
GUI components and menu based J2ME Applications.
Code sample to Send SMS from a J2ME application.
Datagrams in J2ME (UDP Programming sample)
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
RMSCookieConnector - Using Cookies in J2ME
Using HTTP vs UDP vs Socket in J2ME
lists, forms, choices, gauges, text fields, text boxes in J2ME
Latest Articles (in J2ME)
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
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
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate