There are some other articles for learning from the god trinea
analyzing tool
1. Android built-in profile (monitor)
Not only can you see the network, you can also see the memory, cpu
2. Provide tools for weak network, test weak network; and wifi situation
3. Intercept network packets for analysis
url
Generally, it is directly requested by the domain name. Although local dns will be cached, it is better to use other methods.
1. Directly request ip
2.http dns
4 major strategies
1. Take it early
2. Priority, for example, if you want to update the UI with data, this priority needs to be higher; for example, if you want to like, you can display the UI first, and then make a post
3. Packaging, such as likes, users may operate blindly, continue to like, cancel likes, you can do a packaging after exiting this page; there are many packaging scenarios, the most important reason is the keep-alive mechanism, not packaging = waste of resources (packaging also saves power)
4. It is not necessary for 100 requests to be made at the same time, and your network will be blocked in an instant. It can be done in batches, 10 by 10.
compressed body
The server sends you the gzip body, and okhttp will automatically parse it, provided you do not specify accept-encoding
okhttp gzip compressed request body
(official source code)
/** This interceptor compresses the HTTP request body. Many webservers can't handle this! */
static class GzipRequestInterceptor implements Interceptor {
@Override public Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {
return chain.proceed(originalRequest);
}
Request compressedRequest = originalRequest.newBuilder()
.header("Content-Encoding", "gzip")
.method(originalRequest.method(), gzip(originalRequest.body()))
.build();
return chain.proceed(compressedRequest);
}
private RequestBody gzip(final RequestBody body) {
return new RequestBody() {
@Override public MediaType contentType() {
return body.contentType();
}
@Override public long contentLength() {
return -1; // We don't know the compressed length in advance!
}
@Override public void writeTo(BufferedSink sink) throws IOException {
BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
body.writeTo(gzipSink);
gzipSink.close();
}
};
}
compressed header
spdy algorithm: deflate
http2.0 algorithm: hpack
http1.0 and 1.1 cannot be compressed, but can be cached manually, the same header is represented by ciphertext id
Condensed text format
binary protobuf
When using json, combine the fields of multiple objects into a list, which can save the redundant field names
Condensed Image Format
The big picture uses webp instead of jpg, and it will be out without webp!
Small image replace png with svg
Incremental update
Mostly used for version upgrade, server bsdiff, client bspatch
cache
Force expire, cache control
Contrast modified, etag
large file
cache
http
multiple connections
job scheduler
Monitor system, sleep, charge, wifi do different measures. For example, automatically download something when wifi
UI first, data second
IdHandler takes the real UI display time and displays the IO cache data first
like