preface

Although the package is favorable, the data is still very expensive. For users, the network traffic is money! Users are used to opening the system’s own APP traffic statistics function, from the perspective of APP, do not want users to see their APP is a large user of traffic, so it is necessary to spend time to know how APP traffic loss. However, the traffic statistics function of the system is only a very rough statistics of the total traffic consumed by each APP (time sharing), but programmers need to carry out a more detailed and multidimensional analysis of the APP’s traffic, so as to optimize the APP’s data traffic targeted, so there are several solutions as follows. (This article is not only about traffic optimization, but also lists various types of performance optimization for Android applications at the end of the article. Please read on.)

1. Data cache

OkHttp cache

If we carefully follow the interfaces in our own project, we will find that there are many interfaces that do not have such high requirements for real-time performance. Using caching can not only save traffic, but also greatly improve the speed of data access.

Our common network libraries, such as OkHttp and Volley, have good caching practices.

In addition, the lack of caching is also bad for user experience. Generally, an App will display an interface with no data after opening, which is actually a poor user experience compared to showing the last data. Let’s focus on the caching practices of OkHttp. First, define a netless interceptor.

The next step is to add an interceptor to OkHttpClient.



After adding no network interceptor, when we open our App without network, we can also get the data of the last time and use the App, so as to improve the user experience.

2. OkHttp cache processing flow

OkHttp’s cache interceptor processes the request as follows.

Expiration time with incremental updates

1. Expiration time

Add an expiration time to the data returned by the server, so that we can check the expiration time every time we request it. If not, we don’t need to re-request it.

2. Incremental update

The specific idea of incremental data update is to add a version concept to the data. Each time the data is received, the version comparison is carried out, and only the data with changes is received.

This will reduce the amount of data that will be transmitted. For example, provincial and configuration data will be updated less often. If you have to request provincial data every time, it will be a waste of traffic.

We only need to update the changed data, because it is more closely related to the server, so I won’t give you an example here.

2. Data compression

1. Gzip

For Post requests, the Body is Gzip compressed, which means that the request is made with the Gzip request header and the server returns with Gzip compression, so that the data stream is compressed.

2. Compress the request header

The request header also occupies a certain volume. If the request header is not changed, we can only pass it once and only need to pass the MD5 value of the last request header in the future. The server will make a cache, and when some information in the request header is needed, it can directly fetch it from the previous cache.

3. Merge network requests

Every network request has redundant information, such as request header, and combining network requests can reduce the transmission of redundant information.

Three, picture compression

1. The thumbnails

The first method of image compression is to use thumbnails in the list first, because showing the original image consumes more memory and traffic, and it makes no sense to show the original image in the list.

Below is the size of the original image compared to the thumbnail, which is 50% the size of the original image and 10% the size of the original image.

2. WebP

Image compression of the second means, is the use of Webp format, the following is the same image in PNG format and Webp format comparison, Webp format of the size of the PNG format of 51%.



3. Luban

For example, when we upload pictures, do a compression, such as a 2M picture in the local, complete upload is not meaningful, will only increase our traffic consumption, it is best to upload after compression.

And do better in the picture compression is Luban, let’s look at the use of Luban.

Start by adding dependencies.

Dependencies {implementation 'top.zibin:Luban:1.1.8'}Copy the code

Then add the compression to the image.

The image below was originally 1.6m in size and has been compressed to 213KB, 13% of its original size.

That’s all for this article, if you want to learn more about Android performance optimization, please read on. (Surprise at the end)

ANR problem analysis

Crash Monitoring Scheme

Start speed and execution efficiency optimization project actual combat

Memory optimization

Optimize the power consumption

Network transmission and data storage optimization

Apk size optimization

The project of actual combat

The above information, there is a need can be in my QQ technical communication group can take self-help, if in study or work encountered problems, the group will have some god to help answer, sometimes you think about a day, as others a few words will be enlightened, group 793544421, can also click here, join our circle, common progress.