android網絡優化之添加緩存給服務器減負

引言:

        在app的不少實用過程當中,常常會出現頻繁的刷新操做,這個刷新不是指去更新ui,而是相似打開界面—關閉界面—再打開—再關閉等。這段過程當中,並無實質的數據變化,可是這個操做過程當中,卻會頻繁的調用接口,初始化數據,而後去刷新UI,固然還有不少其餘場景。這類頻繁的無效的數據訪問,無疑會增長網絡請求的次數,給服務器帶來壓力,在弱網環境下,也會帶來卡頓等很差的體驗。php

需求分析:

        有沒有一種方法,可以緩存首次成功的數據訪問,而後定個有效期,在這個有效期內,若是還有同一網絡請求,則直接從緩存中獲取返回值。這樣,能夠在這個有效期內,減少訪問服務器的次數。當用戶量大的狀況下,能夠給服務器明顯減負。css

解決方案:

        通過查找,還真找到一個有效的方案來解決上述問題。html

public class NetworkInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Response response = chain.proceed(request);
        int maxAge = 60;  //有效期

        return response.newBuilder()
                .removeHeader("Pragma")
                .removeHeader("Cache-Control")
                .header("Cache-Control", "public, max-age=" + maxAge)
                .build();
    }
}

使用方法:java

 private void testOnlyRequest() throws Exception{

        File cacheFile = new File(this.getApplication().getCacheDir(), "caheData");
        //設置緩存大小
        Cache cache = new Cache(cacheFile, 1024*1024*14);

        final   OkHttpClient client = new OkHttpClient.Builder()
                .retryOnConnectionFailure(true)
                .connectTimeout(15,TimeUnit.SECONDS)
                .addNetworkInterceptor(new NetworkInterceptor())
                .cache(cache)
                .build();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Retrofit retrofit= new Retrofit.Builder()
                        .baseUrl("http://fy.iciba.com/")
                        .client(client)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                GetRequest_Interface request = retrofit.create(GetRequest_Interface.class);
                Call<Translation1> call=  request.getCall_1();
                Translation1 translation1 = null;
                try {
                    translation1 = call.execute().body();
                    translation1.show();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

補全下代碼:ajax

public interface GetRequest_Interface {
    // 網絡請求1
    @GET("ajax.php?a=fy&f=en&t=zh&w=welcome")
    Call<Translation1> getCall_1();
 }
public class Translation1 {
    private int status;
    private content content;
    private static class content {
        private String ph_en;
        private String ph_am;
        private String ph_en_mp3;
        private String ph_am_mp3;
        private String ph_tts_mp3;
        private String[] word_mean;
    }

    //定義 輸出返回數據 的方法
    public void show() {

        Log.d("RxJava", "翻譯內容 = " + content.word_mean[0]);

    }

    public void show(Context context){
        Toast.makeText(context,"翻譯內容 = " + content.word_mean[0], Toast.LENGTH_SHORT).show();
    }
}
怎麼調用就不講了。代碼是最好的老師
相關文章
相關標籤/搜索