解析android项目中的网络请求

项目一

项目中网络请求偶有出现:

日志一

 D/libc-netbsd: [getaddrinfo]:  hostname=api1.51zhenghao.cn; servname=(null);   
app_uid=10332; ai_addrlen=0; ai_canonname=(null); ai_flags=1024; 
ai_family=0 from prox result 0

日志二

W/System.err: java.net.UnknownHostException: 
Unable to resolve host "api.51zhenghao.cn": No address associated with hostname

网络框架使用的是

    // 联网请求框架okgo
    compile 'com.lzy.net:okgo:3.0.3'

该网络框架源码地址
网络框架说明

日志二问题解决办法方案
一:Unable to resolve host "api.51zhenghao.cn": No address associated with hostname

搜索解决方案:如果将配置文件中的 android:required 设置为true 执行以上代码的时候会报错 ,目前原因未知

查看配置文件AndroidManifest.xml,定位到如下代码,我也不清楚添加这个什么意义(不是我写的)。

    

修改之后,基本没有出现DNS解析不了的问题了。

二:

项目二

网络请求框架Retrofit2.1.0

引入框架

    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'

有网无网络先读缓存中的数据

        //retrofit(非异步)网络请求框架,与RxJava(异步框架)结合使用
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(getOkHttpClient())
                .addConverterFactory(customGsonConverterFactory)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())// 把 Retrofit 转成 RxJava 可用的适配类。
                .build();

    private OkHttpClient getOkHttpClient() {
        Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);//缓存文件为50MB
        httpClientBuilder.cache(cache);
        httpClientBuilder.addNetworkInterceptor(new Interceptor() {
          @Override
          public Response intercept(Chain chain) throws IOException {
              Request request = chain.request();

              request = request.newBuilder()
                     .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
                     .addHeader("Accept-Language", "zh-CN,zh;q=0.8")
                     .addHeader("Connection", "keep-alive")
                     .addHeader("Host", "api.51zhenghao.cn")
                     .build();
              Response response = chain.proceed(request);
              response = response.newBuilder()
                          .header("Cache-Control", "public, max-age=" + maxAge)//单位(秒)
                          .removeHeader("Pragma") //清除头信息,因为服务器如果不支持,会返回一些干扰信息
                          .build();
            return response;
            }
        });
        okHttpClient = httpClientBuilder.build();
        return okHttpClient;
    }

有网络从网络获取数据,无网络从缓存中读取数据

    private OkHttpClient getOkHttpClient() {
        Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);//缓存文件为50MB
        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
        httpClientBuilder.cache(cache);
        //设置网络请求缓存策略
        httpClientBuilder.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                if (NetUtil.isConnected(BaseApplication.getContext())) {
                    request = request.newBuilder()
                            .cacheControl(CacheControl.FORCE_NETWORK)//有网络时只从网络获取
                            .build();
                } else {
                    request = request.newBuilder()
                            .cacheControl(CacheControl.FORCE_CACHE)//无网络时只从缓存中读取
                            .build();
                }
                Response response = chain.proceed(request);
                if (NetUtil.isConnected(BaseApplication.getContext())) {
                    response = response.newBuilder()
                            .build();
                } else {
                    response = response.newBuilder()
                            .removeHeader("Pragma")
                            .removeHeader("Cache-Control")//清楚响应体对Cache有影响的信息
                            .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)//单位(秒)
                            .build();
                }
                return response;
            }
        });

        okHttpClient = httpClientBuilder.build();
        return okHttpClient;
}

抛出异常retrofit2.adapter.rxjava.HttpException:HTTP 504 Unsatisfiable Request (only-if-cached)

你可能感兴趣的:(解析android项目中的网络请求)