Optimization of network performance for the Android introductory development tutorial

Source: Internet
Author: User
Tags gzip download

when I was developing my Android at the Wheat College, the Android development teacher at Wheat College said that the Android development process often involved performance optimization, which should be optimized from the basic, network, testing and other aspects. Now let's talk about the optimization of the network performance of Android development.


1) Avoid frequent network requests
when accessing the server side, establishing the connection itself will take more time than the transmission, if not necessary, and do not separate the things that an interaction can do into multiple interactions (this needs to be reconciled with the server side). Effective management of service background services is equivalent to a continuous operation of the acitivity, if the development of the program backstage will be a service to constantly update the data on the server, when the data is not updated to let it sleep, this way is very power consumption, usually, You can use Alarmmanager to start services on a timed basis. The first 30 minutes are executed once, as shown below.

Alarmmanageralarmmanager = (alarmmanager) context.getsystemservice (context.alarm_service); Intent Intent = new Intent (context, myservice.class); Pendingintentpendingintent = Pendingintent.getservice (context, 0, intent, 0); Long interval = Dateutils.minute_in_ Millis * 30;long firstwake = system.currenttimemillis () + interval;am.setrepeating (Alarmmanager.rtc,firstwake, Interval, pendingintent);

2) data compression
transfer data compressed the current large department of the Web site are supported gzip compression, so in the large data volume download, as far as possible to use gzip download, can reduce network traffic, is generally compressed before the data size of about 30%.

HttpGet request = new HttpGet ("http://..... "); Httpresponseresp = new Defaulthttpclient (). Execute (Request); httpentity entity = response.getentity (); InputStream compressed = Entity.getcontent (); inputstreamrawdata = new Gzipinputstream (compressed);



3) Using the thread pool
thread pool, divided into core thread pool and normal thread pool, download pictures and other time-consuming tasks placed in the normal thread pool, avoid time-consuming tasks blocking the thread pool, resulting in all asynchronous tasks must wait.

4) Select the appropriate format for data transmission

where tree parse is DOM parsing Event/stream is the sax way to parse.
Obviously, the way to resolve the flow is more efficient, because DOM parsing is done after reading the entire document and then organizing it according to the hierarchy of nodes. And the flow of the way is the edge of reading data edge resolution, after reading the data, the resolution is complete. In terms of data formats, JSON and PROTOBUF are significantly more efficient than XML, and XML and JSON are well known. From the above figure can be concluded that the use of Sax equal reading Edge parsing method to parse the data, for mobile devices, it is best to use a lightweight data format such as JSON is preferred.

5) Set timeout period

Set the connection time-out and response time-out. The HTTP request is divided into whether it can be cached and not cacheable according to the business demand, then, in the network-free environment, the cached HttpResponse is still used to browse some data and realize offline reading.


For the Android Starter Development tutorial go to:Http://www.maiziedu.com/course/android/


Optimization of network performance for the Android introductory development tutorial

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.