Network Programming on mobile phones

Source: Internet
Author: User

Network Programming on mobile phones

Javami Studio
Chen yuefeng

From: http://blog.csdn.net/mailbomb

 

With the development of mobile phone programming, mobile network programming has become a basic mobile phone software development skill. Although compared with PC network programming, mobile network programming is relatively simple, most of them are client network programming, but there are still a series of problems in actual use.

I think it has been a while for mobile network programming. Here, I will share some of my mobile network programming experience with you. If you have any shortcomings, please correct them and improve them together.

The descriptions below are applicable to various mobile phone development technologies, and the code section is based on the J2EE technology.

1, Network Programming Architecture

The basic architecture of mobile network programming is the listener (listener) mode. The basic structure includes three parts:

L Interface Class (or logic class)

Public democanvas
Extends canvas implements httplistener {

Public void paint (Graphics g ){

.....

}

 

/** Listener callback Method */

Public void doresponse (byte [] B ){

 

}

}

L Listener Interface

Public interface
Httplistener {

Public void doresponse (byte [] B );

}

L Network thread

Public class
Httpthread extends thread {

Private string url = "127.0.0.1/test/1. jsp ";

Private byte [] B;

Private httplistener listener;

Public httpthread (byte []
B, httplistener listener ){

This. B = B;

This. Listener = listener;

Start ();

}

 

Public void run (){

// Networking code

// Call the listener method to handle feedback

Listener. doresponse (B );

}

}

The biggest advantage of using this basic structure is that the network Thread class is independently processed and only responsible for data transmission. The data generated and feedback data are transmitted through the listener. When the network is connected, you only need to implement the listener for the corresponding class. The code to be written when the network is connected is:

New
Httpthread (B, this );

In this way, the network connection is implemented through the httpthread class, And the received data is handed over to the current interface class for processing through the this listener, so that data can be processed conveniently.

Of course, you can use the doresponse method to implement a similar doerror Method for error handling.

2, Access Point Processing

No matter what mobile phone development technology you use, you cannot bypass access point processing for network programming.

A) let's take a look at the access points of each operator:

A) China Mobile (chinamobile)

1) cmnet: direct connection, applicable to various network methods, such as socket and HTTP

For example, the HTTP connection code is:

Httpconnection Hc =
(Httpconnection) connector. Open (

"Http: // 127.0.0.1/test/1. jsp ");

2) cmwap: proxy connection, applicable only to HTTP. To put it bluntly, it is an HTTP proxy server.

Proxy server address: 10.0.0.172

Port: 80

For example, the HTTP connection code is:

Httpconnection Hc =
(Httpconnection) connector. Open (

"Http: // 10.0.0.172: 80/test/1. jsp ");

HC. setrequestproperty ("X-Online-host ",
"127.0.0.1 ");

Note: cmwap cannot be tested using a simulator or a local machine. It must be tested on a mobile phone.

B) China Unicom (chinaunicom)

1) cunet, same as cmnet

2) cuwap, same as cmwap

C) China Telecom (chinatelecom)

1) ctnet, same as cmnet

2) ctwap

Proxy server address: 10.0.0.200

Port: 80

I am confused why ctwap is not the same as the proxy server address used by cmwap and cuwap. It is very depressing! Explanation.

In short, the original intention of each operator to divide access points is to build a network, hoping to charge fees through the content, something similar to the mobile Dream network. Unfortunately, it is not very successful. I still hope to cancel the Access Point as soon as possible.

B) Access Point processing:

For developers, since mobile phones are divided into access points, and the default access points are different for different users, smartphone users can also select access points, so they must handle network programming, next we will discuss the handling methods:

A)
User settings

This method provides an option in the software so that you can set the network mode. The default value is cmnet. If the connection fails, you can set it to cmwap on the settings page.

B)
Automatic Adaptation

This method is used to automatically identify the access point when the software starts. The identification principle is to connect. If the connection fails, this is not the case. For example, cmnet is used for connection by default. If the connection fails, cmwap is used. Another way is to connect to the Internet using both cmwap and cmnet when the network connection is started for the first time.

3, HTTP or socket?

HTTP does not support persistent connections. The advantage is that it is common and various access points can be used.

The socket method supports persistent connections, but only access points such as cmnet can be used.

So my advice is to use HTTP if your software needs to be generic. Slow down!

4, Billing page processing

In order to prevent user Misoperation and generate traffic, mobile may encounter a WML interface with a fee prompt when connected to the Internet in some cases. This interface also appears when connected to software.

Because the data format on the billing page is different from the data format of the software, you must process the billing page when connecting to the Internet. The solution is to discard the reconnect once if the billing page is found.

The billing page is usually determined by the Content-Type of the response header data. Therefore, when processing the page, my server is set to a specific content-type, for example, AAA, the Content-Type in the feedback is read every time. If it is not AAA, It is the billing page. just reconnect once. Example code:

String S =
HC. getheaderfield ("Content-Type ");

If (S. Equals ("AAA "))){

} Else {

Hc = (httpconnection)
Connector. Open ("http: //" + URL );


......

}

5, Network timeout Processing

The network connection times out. If you stay on the waiting page, it will become stuck. Therefore, you generally need to process the network timeout.

If the network technology you are using times out, congratulations, you can leave it to the system. You only need to set a time-out period. If not, it is not difficult to handle it by yourself.

The solution is as follows:

Generally, there is a thread in your program (if there is no thread, you can only create one). When you start to connect to the Internet, use this thread for instant execution. If the time reaches the timeout time, for example, 30 seconds, "stop" the online thread.

However, generally, online threads cannot be stopped, so we can adopt a flexible method, that is, if the timeout occurs, the online data feedback will be discarded. The sample code is as follows:

Public class
Httpthread extends thread {

Public Boolean isabandon;

Public void run (){

// Networking code

// Call the listener method to handle feedback

If (! Isabandon ){

Listener. doresponse (B );

}

}

}

In this way, if the timeout occurs, you only need to set:

HTTP. isabandon =
True;

HTTP is a networked thread object.

6, Network interruption handling

On the "wait for network connection" page, a "cancel" button is usually set to enable the user to cancel the connection when the network is connected. The processing method is the same as 5. Network timeout processing principle, it usually involves page switching.

7, XML?

Many people may encounter the transmission of data in XML format. We do not recommend this in the current 2G network, or design a compact data format, both data volume and transmission speed are better.

 

 

Due to my limited level, I hope you can make more bricks!

Chen yuefeng, Java Studio

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.