OPhone-based Widget online instance development (1) (1)

Source: Internet
Author: User

Overview

Next we will use a real-time update rate to view the program to illustrate the specific development process of the online widgets on the OPhone. Our instance is called exchange rate communication. We can view the exchange rate relationships between various mainstream currencies. Let's take a look at the overall effect.

 

Basic knowledge

To implement the Widget networking example, we need to know about Ajax and DOM in two aspects. Below we will briefly introduce the content we need to use in this example. AjaxAsynchronousJavaScriptandXML, Asynchronous JavaScript and XML) Ajax uses the JavaScript-based XMLHttpRequest object to send asynchronous requests to the Web server to obtain data.

  • Android Widget Development Series
  • Android Widget development details
  • How to Build Activi in Android Widget Development
  • Comprehensive Understanding of Web Widget Development
  • JIL Widget development tutorial

1) There are two request methods:

Get: query data from the server

Post: send data to the server

2) XMLHttpRequest object methods include:

Open "method", "URL", "async"): sets the request method.

SetRequestHeader: Set the header information of the sent request, including the request Content format and length. The most common method is to set the request format, such as setRequestHeader ("Content-type ", "text/xml ");

Send (): send a request

3) attributes of the XMLHttpRequest object include:

Onreadystatechange: event processing triggered when the status changes

ReadyState: the preceding five states: 0: not initialized; 1: loading; 2: loading; 3: interaction; 4: Completed. We can handle different events in different States. For example, if status 2 is loaded, we can register an onLoading function. In status 4, we can register onComplete)

ResponseText: returned text, which can be directly assigned to an element in html in the form of innerHTML.

ResponseXML: returned content in xml format

Status: the Status of the request. There are many requests. Among them, 200 is required, indicating that the request is successful. The requested document or file has been found and is returned correctly, we will also pay attention to some status of request failure, the most common example is 404, indicating that the requested file is not found

StatusText: text description corresponding to the preceding status, for example, 200 corresponds to OK, 404 corresponds to NotFound

Now let's create a basic Ajax request and Data Processing framework.

The basic content is the first choice to create an XMLHttpRequest type object, then set the basic parameters, then determine the Request status, and provide a callback function to enhance data processing.

We can create a network. js file and copy the following code.

 
 
  1. /*** Make a request for an XML document.
  2. ** @ Param url * the url for the 'get' call
  3. * @ Param onSuccess * a call-back for successful completion of request * @ param onError
  4. * A call-back for request failure */var globalReq; function requestXML (url, onSuccess, onError)
  5. {// Create an XMLHttpRequest objectGlobalReq=NewXMLHttpRequest (); if (globalReq)
  6. {// Send the data request globalReq. open ('get', url, true) asynchronously to the given url in Get mode );
  7. // Set the data format of the request to xml globalReq. setRequestHeader ("Content-type", "text/xml ");
  8. // The asynchronous callback function traces changes in the Request status.GlobalReq. onreadystatechange=Function()
  9. {// If the status is not 4, that is, if the request is not completed, it will not be processed and will be returned directly until the whole request process if (globalReq. readyState! = 4)
  10. {Return;} // if the request process is completed, determine whether the returned status indicates success, that is, whether it is 200 if (GlobalReq. status= 200)
  11. {// If the request data is successful, onSuccess (globalReq. responseXML) will be processed in the xml format of the request in the callback function onSuccess );}
  12. Else {// if the request data fails, print the onError ("Web data unavailable" + globalReq. statusText) Cause in the onError callback function );}};
  13. // Send the request globalReq. send (null );}}
  14.  

With the previous introduction and detailed comments in the code, we should be able to understand requestXML, a function requesting xml data in the form of Ajax. It should be noted that, because we are developing the JILWidget of OPhone, we do not have to add compatibility judgments like the ones used in the desktop browser. At the same time, we are only interested in the status of successful or failed data requests after the request is completed, therefore, there are only two callback functions: onSuccess and onError.

In this way, when we need to request network xml data, we can include the above network in the html file. js file, and then upload the request URL and the success and failure call callback function.


Related Article

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.