Android face question and answer (i)

Source: Internet
Author: User

Handler vs Asynctask
Asynctask, a lightweight asynchronous class provided by Android, can directly inherit Asynctask, implement asynchronous operations in a class, and provide interface feedback about the current level of asynchronous execution (UI progress updates can be implemented through an interface), and finally feedback the results of the execution to the UI main thread.
Advantages:
L Simple, fast
L Process controllable
Disadvantages:
L becomes complex when multiple asynchronous operations are used and UI changes are required.

Handler asynchronous implementation involves Handler, Looper, Message,thread four objects, the implementation of the asynchronous process is the main thread boot thread (child thread) thread (child thread) Run and generate Message-looper get a message and pass it to handler,handler one by one to get the message in Looper and make UI changes.
Advantages:
L Clear structure, clear function definition
L Simple and clear for multiple background tasks
Disadvantages:
L in a single background asynchronous processing, appear too much code, the structure is too complex (relativity)

JSON vs XML
XML definition
The Extensible Markup Language (extensible Markup Language, XML), which is used to tag electronic files with a structured markup language that can be used to tag data, define data types, is a source language that allows users to define their own markup language. XML uses the DTD (document type definition) document type definition to organize the data, a unified format, cross-platform and language, and has long been recognized as a standard in the industry.

JSON definition
JSON (JavaScript Object Notation) is a lightweight data interchange format with good readability and easy-to-write features. Data exchange between different platforms is possible. JSON uses a highly compatible, fully
Independent of the language text format, but also have a similar to the C language habits (including C, C + +, C #, Java, JavaScript, Perl, Python, etc.) the behavior of the system. These features make JSON an ideal data exchange language.

Common advantages:
are data formats for text representations that can exchange data across platforms and systems

(1). Readability.
JSON and XML are basically the same readability of data, JSON and XML are comparable in readability, while the proposed syntax, one side of the canonical label form, XML readability.
(2). Extensibility aspects.
XML is inherently well-extensible, and JSON, of course, has nothing to do with XML extension, not JSON.
(3). Coding difficulty.
XML has a wealth of coding tools, such as dom4j, Jdom, and so on, JSON also has json.org provided tools, but JSON is much easier to encode than XML, even if the use of tools can write JSON code, but to write good XML is not easy. In general, XML is more appropriate for tagging documents, while JSON is more suitable for data interchange processing.
(4). Decoding difficulty.
Parsing the XML takes into account the child node parent, which makes the person dizzy, and the JSON is almost 0 difficult to parse. This is a little bit of XML output.
(5). degree of popularity.
XML has been widely used in the industry, and JSON has only just begun, but in the specific area of Ajax, the future development must be XML to be located in JSON.
(6). Analytical means.
JSON and XML also have rich parsing tools.
(7). Data volume aspect.
JSON, with respect to XML, has a small volume of data and is faster to pass.
(8). Data interaction aspects.
The interaction between JSON and JavaScript is more convenient, easier to parse, and better to interact with the data.
(9). Data description aspects.
JSON is less descriptive of the data than XML.
(10). Transmission speed.
JSON is much faster than XML.

SQLite operations
Please inherit the Sqliteopenhelper implementation:
1). Create a database with a version of "Diaryopenhelper.db" of 1,
2). Create a "diary" table at the same time (contains a _ID primary key and self-growing, topic character 100
Length, content character type 1000 length)
3). When the database version changes, delete the diary table and recreate the diary table.

public class DBHelper extends Sqliteopenhelper {public final static String DATABASENAME = "diaryopenhelper.db";p ublic Fina l static int databaseversion = 1;//CREATE Database public dbhelper (Context context,string name,cursorfactory factory,int version) { Super (context, name, Factory, version);} Creating an institutional file such as a table public void OnCreate (Sqlitedatabase db) {String sql = "CREATE TABLE Diary" + "(" + "_id Integer primary key autoincr Ement, "+" topic varchar (+), "+" content varchar ("+") ";d b.execsql (SQL);} Call this method if the database version is updated public void Onupgrade (sqlitedatabase db,int oldversion,int newversion) {String sql = "DROP table if Exis TS Diary ";d b.execsql (SQL); this.oncreate (db);}}

What kinds of XML-parsing classes are available in Android? What are the official recommendations? and their principles and differences.
There are three main ways of parsing XML, SAX, DOM, pull.

The DOM parsing XML file stores all the contents of the XML file in memory in the document tree, and then uses the DOM API to traverse the XML tree, retrieving the required data, primarily for the PC, which can be well combined with XPath in terms of lookup, if the amount of data is not highly recommended.
Pros: The code for parsing XML using the DOM is more intuitive and simpler than a sax-based implementation. Disadvantage: The XML file must be stored in memory, so the memory consumption is large, not applicable to mobile devices such as Android.

The Sax parsing XML file is event-driven, that is, his read is one-way, does not need to parse the entire document, in the process of parsing the document in the order of content, sax will determine whether the currently read characters conform to a part of the XML syntax, if the match will trigger the event, is actually some callback method, Then the judgment is processed.
Advantages: Fast resolution, low memory consumption, convenient parsing properties, suitable for mobile devices such as Android. Cons: Handling is not very convenient for nested multiple branches.

The pull parser is an Android built-in parser that parses XML files, running in a similar way to sax parsing, except that the resulting event is a number, not a method, so you can use a switch to process an event of interest. Pull parser is good for node processing, also very memory-saving, the official push using the Pull parser parsing XML files, and the Android system itself used in the XML file is also using the pull parser to parse.
Sax, pull also a little difference, pull parsing with a while loop, can jump out at any time, and sax is not, sax is as long as the parsing, it must be resolved to complete.

Android face question and answer (i)

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.