How to enable and manage data connections in ophone 1.0 and 1.5

Source: Internet
Author: User
Document directory
  • Ophone platform development, 14:28:28
Ophone platform development, 14:28:28 tags: network connection Application Data Connection

 


This article describes how ophone applications manage network connections. The attachment provides a code library for Development reference and example. It encapsulates common network requests and data acceptance methods, making development of common network applications much more single, this allows network program developers to focus on application logic. The attachment also provides a running application as a sample code for network development.
The network development method described in this article is applicable to the two released versions of ophone, namely ophone.
1.0 and ophone 1.5.

Access Point management for ophone network connection
Ophone
The platform allows multiple applications to reuse grouped data. Different applications can enable different network connections as needed, in this way, we can perform network tasks such as Browsing webpages, receiving MMS messages, and sending emails simultaneously in different ophone applications. You can configure the network access point through the ophone operation. Select "configuration" and "Data Connection" from the icon on the first screen for configuration. The configuration is saved as system information. Each application can query the configuration details of these access points, select an access point to start network connection.

These two configurations are different in ophone 1.0 and ophone 1.5.

The ophone 1.0 Configuration window is as follows. The Network Access Point Selection window is used to edit the details of a network access point.

 

Figure 1: ophone 1.0 access point list
Figure 2: ophone 1.0 Access Point editing

The ophone 1.5 Configuration window is as follows. He is better than ophone
1.0 more than the first-level access point type list, multiple access points can be configured for each type. Access Points under a type list can be considered as a group of access points of the same type. This also determines the ophone
1.5 there are some differences in the management and connection use of network access points. For more information, see the following section.

Figure 3: ophone 1.5 access point group Figure 4: ophone
1.5 access point list Figure 5: ophone 1.5 Access Point configuration

Ophone network connection process

Describes the complete process for processing a network data request on ophone.


The process is divided into the network connection part and the data request receiving part. The Data Request part can use java standard network requests or the Apache
Httpclient interface, which is not much different from general Java network development. For details, refer to the article "How to Write network applications on the ophone platform" written by Wang Wei on the ophone Sdn.


The network connection is used to establish network data channels on devices. For example, in the GPRS environment, a group data (PDP) connection is established. He is the biggest difference from General Java network development, that is, there can be no "network connection part" on the simulator running on the PC ", however, if you want your application to request network data on the actual ophone, you must understand this step.


There may be multiple data channels on a mobile device, for example, there may be multiple data channels at the same time, such as GPRS, WiFi, USB virtual network interface, Bluetooth virtual network interface, etc, therefore, a mobile phone may have multiple access points for network applications. The application can query the configuration of these access points and select a network connection.


On the ophone system, you can connect to/use multiple access points at different times when the application is running, but at a certain time of network connection, only one data channel connected to the network can be activated. To switch to another network access point, you must initiate a new network connection.

It must be noted that ophone 1.0 and ophone
1.5 The methods for initiating a network connection are different. This also causes program transplantation problems to some extent, increasing the complexity and workload of network application development.

Ophone
1.5 Use the startusingnetworkfeature () method in the android.net. connectivitymanager class to activate network connections. Network status notifications are transmitted and accepted through an intent message event_data_state_changed. Ophone
1.0 use the openconnection () method in the android.net. dataconnection class to activate the network connection. Use the setnetworkstatuslistener () method to set the receiving interface of the network status.

Ophone 1.5 adds the access point type (APN) to the Access Point configuration of ophone 1.0.
Type). Access Points of the same type are grouped into access point groups. Ophone
1.5 when a network connection is initiated, the parameter is set to the access point type. During the connection, all access points in the Access Point group are connected until the network connection is successful. For example, "Figure 6: ophone
1.5 network configuration shown in the Access Point list. If the first APN connection fails during connection, the second APN will be used before the connection operation ends. While ophone
1.0 the connection parameter passed is the access point id. One ID corresponds to only one Access Point configuration item.

Currently, common network access points in China Mobile's GSM network have cmwap for different GPRS access points,
Cmnet,
Different Access Point configuration items set by cmmail. Generally, each access point may initiate a network connection. However, depending on the operation of the mobile network, different data services may have different charges for different access points.

Of course there are some other nuances. For example, the most common cmwap,
In addition to billing differences, the two access points of cmnet use cmwap access points. to connect a mobile phone to a server on the internet, you must use an HTTP proxy, that is, after using a cmwap access point, the application can only initiate standard HTTP requests. The proxy server address configured for China Mobile is 10.0.0.172 and port 80. In addition, if you use China Mobile's cmwap access point, you must face such a problem. Generally, when the network connection is established for the first time, the first content after an HTTP request is sent is not actually what you need, but instead of a WML-encoded billing reminder page. The solution is that you may need to analyze the returned content. If it is incorrect, you need to re-initiate a network request to obtain real network data (if you are developing a browser, you can also directly display the received WML page, so that users can receive a billing reminder, click the link on the page to skip the billing Reminder ).

Network Development of ophone applications
Ophone
Ophone 1.5
1.0. Here we try to use a common interface encapsulation to simplify the porting process of network applications between different versions. And consider the simplest interface design to make network calls more convenient.

1. First copy the two required help library files to the development test below:

Ophone
1.5: dataconnection_helper_v15.jar and dataconnection_helper_httpget.jar;

Ophone
In 1.0, dataconnection_helper_v10.jar and dataconnection_helper_httpget.jar are used.

2. Configure the ophone project and add the two libraries to the Eclipse project under development. As shown in:

First, select "Java build path" on the left, select "Libraries" on the right tab, and then click "add external ".
Jars ...". Select the copied JAR file and confirm whether it is successfully added.

Go to the configuration page and select "order and"
Export, select the two newly added jar files, and allow the project to export the content of both jar libraries at the same time during compilation.

3. Use the code library provided by the Helper library in the project code. The dataconnectiondemo program for the example is as follows:

 

View plaincopy to clipboardprint?
  1. // The following is the name of the database referenced by ophone 1.5,
  2. // For ophone 1.0, use OMS. dataconnection. helper. V10 .*
  3. Import OMS. dataconnection. helper. v15 .*;
  4. Import OMS. dataconnection. helper. httpget. datahttp;
  5. Public class dataconnectiondemo extends activity {
  6. Public void oncreate (bundle savedinstancestate ){
  7. Super. oncreate (savedinstancestate );
  8. Setcontentview (R. layout. Main );
  9. // Initialize the access point to query the help Class Object
  10. Mqueryaplist = new queryaplist (this );
  11. // Obtain the title of each access point
  12. String [] aplisttitle = mqueryaplist. getaptitle ();
  13. // Generate the network connection help object
  14. Mconnecthelper = new connectbyap (this );
  15. // Obtain the ID of the Access Point at the specified serial number.
  16. Final int apid = mqueryaplist. getapid (position );
  17. // Create a network connection thread
  18. Runnable = new runnable (){
  19. Public void run (){
  20. // Obtain the proxy server information configured for the Access Point
  21. String proxyhost = mqueryaplist. getapproxy (position );
  22. Int proxyport = mqueryaplist. getapproxyport (position );
  23. // Connect and set the network intervention points currently in use
  24. // The function is blocked until the network connection ends. The returned value indicates whether the connection is successful or failed.
  25. Boolean result = mconnecthelper. Connect (apid, 10000 );
  26. If (! Result)
  27. Return;
  28. // At this point, the network connection is successful. You can start to request and receive network data for Java.
  29. // However, a self-encapsulated HTTP data request class datahttp is used to simplify data receiving.
  30. // Datahttp actively requests HTTP data compressed by gzip or deflate, and automatically decompress the received compressed data,
  31. // Try to read the character encoding information of the content from the HTTP header.
  32. // First, generate a network request
  33. Datahttp = new datahttp (requesturl
  34. , "*/*", "Ophone 1.5", proxyhost, proxyport)
  35. {
  36. // Customize the network notification received by the new data for obtaining the data transmission status
  37. @ Override
  38. Protected void ondatareceived (INT len_read, int size, int total ){
  39. // Show data processing status
  40. Printlog (title, String. Format ("received data: % d (% d/% d )"
  41. , Len_read, size, total ));
  42. Super. ondatareceived (len_read, size, total );
  43. }
  44. };
  45. // Then, send the request network and receive the returned data
  46. Printlog (title, String. Format ("start getting data through: % s, % s, % s: % d"
  47. , Aptype, requesturl, proxyhost, proxyport ));
  48. Try {
  49. Datahttp. Connect ();
  50. } Catch (malformedurlexception e ){
  51. E. printstacktrace ();
  52. } Catch (ioexception e ){
  53. E. printstacktrace ();
  54. }
  55. // Read the returned data
  56. // Call the following method to decompress the compression encoding and automatically identify the character encoding.
  57. String page = datahttp. getresponsestring ();
  58. If (page = NULL)
  59. Printlog (title, "failed indexing data from:" + requesturl );
  60. Else {
  61. String Strout = (page. Length () <= 200)
  62. ? Page
  63. : Page. substring (0,195) + "....";
  64. Printlog (title, String. Format ("finished processing ing data (% db )"
  65. ", Page length in char: % d/n % s/n =============="
  66. , Datahttp. Body. length, page. Length (), Strout ));
  67. }
  68. } // End of run ()
  69. }; // End Defining class runnable
  70. // Start the network connection and Data Reading thread
  71. New thread (runnable). Start ();
  72. } // End oncreate ()
  73. } // End Defining class dataconnectiondemo
// The following is the name of the database referenced by ophone 1.5. // use OMS for ophone 1.0. dataconnection. helper. v10. * import OMS. dataconnection. helper. v15. *; import OMS. dataconnection. helper. httpget. datahttp; public class dataconnectiondemo extends activity {public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // initialize the object of the Access Point query help class mqueryaplist = new queryaplist (this); // obtain each access point Point title string [] aplisttitle = mqueryaplist. getaptitle (); // generate the object mconnecthelper = new connectbyap (this) for network connection help; // obtain the ID final int apid = mqueryaplist of the Access Point at the specified serial number position. getapid (position); // the network connection creation thread runnable = new runnable () {public void run () {// obtain the proxy server information configured by the Access Point string proxyhost = mqueryaplist. getapproxy (position); int proxyport = mqueryaplist. getapproxyport (position); // connect and set the currently used network intervention point // function 1 Block directly until the network connection ends. The returned value indicates whether the connection result is successful or fails. boolean result = mconnecthelper. Connect (apid, 10000); If (! Result) return; // at this point, the network connection is successful, you can start to request and receive Java universal network data. // However, a encapsulated HTTP data request class datahttp is used to simplify data receiving. // datahttp will actively request gzip or deflate compressed HTTP data, and automatically decompress the received compressed data, // and try to read the character encoding information of the content from the HTTP header. // First, generate the network request datahttp = new datahttp (requesturl, "*/*", "ophone 1.5", proxyhost, proxyport) {// customize the network notification received by the new data for obtaining the data transmission status @ override protected void ondatareceived (INT len_read, int size, int total) {// show data processing ing status printlog (title, String. format ("received data: % d (% d/% d)", len_read, size, total); super. ondatareceived (len_read, size, total) ;}}; // then, send the request network and receive the returned data PRI Ntlog (title, String. format ("start getting data through: % s, % s, % s: % d", aptype, requesturl, proxyhost, proxyport); try {datahttp. connect ();} catch (malformedurlexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} // read the returned data // call the following method to decompress the compression code and automatically recognize the string page = datahttp. getresponsestring (); If (page = NULL) printlog (title, "failed indexing data from :" + Requesturl); else {string Strout = (page. Length () <= 200 )? Page: page. substring (0,195) + ".... "; printlog (title, String. format ("finished processing ing data (% db)" ", page length in char: % d/n % s/n ============== ", datahttp. body. length, page. length (), Strout) ;}// end of run ()}; // end Defining class runnable // start the network connection and Data Reading thread new thread (runnable ). start () ;}// end oncreate ()} // end Defining class dataconnectiondemo

Attachment Reference Library usage
See the attached javadoc document.

Running Effect of the attachment sample code

Displays the running effect of the sample program. Cmnet access point is used for network connection, and HTTP proxy is not set. The size of the obtained data is 9516 bytes, and the extracted content contains 30649 characters. After six reads and waits, the network data transmission speed can also be calculated.


Displays the content on the billing reminder page after you connect to the cmwap access point for the first time. The proxy 10.0.0.172: 80 is used, and the obtained content length is 488.
Byte. The content is the WML page.

 


After you re-use the cmwap access point to obtain data, the content is successfully obtained, and the received file on the network is 31195 bytes. We can see from the effect of the demo running time that the HTTP Proxy server configured for the cmwap access point at the demo running time does not support HTTP Content compression encoding transmission.
Therefore, the amount of data transmitted and received is the size of the original webpage before compression encoding is not transmitted.

Source code:

Bin.zip
Dataconnectiondemo. tgz
Document-DataConnectionHelper.zip

 

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.