使用网络的场景概述
数据:Api、资源包(升级包、H5、RN)、配置信息
图片:下载、上传
监控:APM相关、单点问题相关
数据缓存
服务端返回加上过期时间,避免每次重新获取
节约流量且大幅度提高数据访问速度,更好的用户体验
OkHttp、Volley都有较好实践
public class NoNetInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request.Builder builder = request.newBuilder();
if(!Utils.isNetworkConnected(PerformanceApp.getApplication())){
builder.cacheControl(CacheControl.FORCE_CACHE);
}
return chain.proceed(builder.build());
}
}
public class RetrofitNewsUtils {
private static final APIService API_SERVICE;
public static APIService getApiService() {
return API_SERVICE;
}
public static final String HTTP_SPORTSNBA_QQ_COM = "http://sportsnba.qq.com/";
static {
OkHttpClient.Builder client = new OkHttpClient.Builder();
Cache cache = new Cache(PerformanceApp.getApplication().getCacheDir(),10*1024*1024);
client.cache(cache).addInterceptor(new NoNetInterceptor());
final Retrofit RETROFIT = new Retrofit.Builder()
.baseUrl(HTTP_SPORTSNBA_QQ_COM)
.addConverterFactory(FastJsonConverterFactory.create())
.client(client.build())
.build();
API_SERVICE = RETROFIT.create(APIService.class);
}
}
增量数据更新
加上版本的概念,只传输有变化的数据
配置信息、省市区县等更新
数据压缩
Post请求Body使用GZip压缩
请求头压缩
图片上传之前必须压缩
优化发送频率和时机
合并网络请求、减少请求次数
性能日志上报:批量+特定场景上报
图片相关
图片使用策略细化:优化缩略图
使用WebP格式图片