Development of networked applications using the J2ME Universal Networking framework

Source: Internet
Author: User
Tags array error handling flush response code thread
Although the current wireless network is not ideal, mobile networking or to our developers not small shock. After all, it's really

It's a magic thing, isn't it? This article will describe how to use the Universal networking framework in the J2ME platform to develop networking applications

Program.
  
First, it must be stated that MIDP requires that any Mobile information device must provide a support through the HTTP protocol

Hold, and like other means of communication such as sockets are device-related. Some phones will support, others won't. Here only

A general description of the HTTP protocol related content, if you do not understand this knowledge, please refer to the HTTP protocol. In

Javax.microedition.io inside is a large number of interfaces, only a connector class, of course, in midp2.0 add a

Support for the push technology, this left to do after speaking. The most important method provided by the connector class is the open () method, which

The return value is connection, you can convert it to get the type you need, such as our HTTP protocol Access service

Manager

void postviahttpconnection (String url) throws IOException {
Httpconnection c = null;
InputStream is = null;
OutputStream OS = null;
int RC;

try {
c = (httpconnection) connector.open (URL);

Set the request method and headers
C.setrequestmethod (Httpconnection.post);
C.setrequestproperty ("If-modified-since", "Oct 1999 19:43:31 GMT");
C.setrequestproperty ("User-agent", "profile/midp-2.0 configuration/cldc-1.0");
C.setrequestproperty ("Content-language", "en-us");

Getting the output stream may flush the headers
OS = C.openoutputstream ();
Os.write ("LIST games\n". GetBytes ());
Os.flush (); Optional, Getresponsecode would flush

Getting the response code would 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 ();
Processtype (type);

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;
}
Process (data);
} else {
int ch;
while (ch = is.read ())!=-1) {
Process ((byte) ch);
}
}
catch (ClassCastException e) {
throw new IllegalArgumentException ("Not a HTTP URL");
finally {
if (is!= null)
Is.close ();
if (OS!= null)
Os.close ();
if (c!= null)
C.close ();
}
}

The above code is I take from API Doc (recommended to read more API Doc).
Here are some of the more important issues in networking, based on your own experience:

We should understand how this works, the mobile phone sends requests through the wireless network to the operator's WAP gateway,

The WAP gateway forwards the request to the Web server, which can be built with cgi,asp,servlet/jsp. After server processing

The response is forwarded to the WAP gateway, which is then sent to the phone by the WAP gateway. The WAP gateway is for our developers

Transparent we don't care about it.

If you don't see a word like thread,runnable on your networking program, your program is not running

Of Because of the network factor, in order to avoid the blockage of the operation. You have to put the networking action on another thread to transport it.

Row, not run on the main thread. It is best to provide users with a waiting interface when networking, such as an animation industry

Surface. The examples I provide below are useless because I want to talk about this alone.

Usually the interface of the networked application is more, it is best we use the pattern of MVC to implement the navigation of the interface.

It's important to think about how you want to deliver your data. You can use the Get method or the Post method,

recommend the latter. Because the Get method can only be transmitted through URL encoding. and post is more flexible, with

DataInputStream, DataOutputStream to use is more convenient. It's important to know how we accept data as a number.

How it is sent over, for example at the client end WRITEUTF (message); Writeint (4); Writeboolean

(true), then the acceptance should be readUTF (); ReadInt (); Readboolean (); if sent over data length is available

, then we can create an appropriate array to accept, if not available we need an array of appropriate capacity to receive

Affected

When networking, must follow the following process to do

1, establish the connection, set the transmission mode recommended post, set method head

2, open the output stream, transfer data to the server

3, to determine the corresponding status code, into different flow control, pay attention to error handling. If OK starts to accept the data

4, close the connection and flow




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.