网络优化(一)缓存优化

除了上面的JobSchedule的网络时机请求的网络优化还有其他优化。

1.请求的缓存。
Http请求是可以做缓存的。

public void openCache(View v){
        try {
            File cacheDir = new File(getCacheDir(), "http");
            long maxSize = 10*1024*1024;//»º´æ´óС£¬µ¥Î»byte
            HttpResponseCache.install(cacheDir, maxSize );
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    
    public void request(View v){
        Log.d(TAG, "~~~~~~~~~~~~~");
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                try {
                    //10.0.2.2
                    HttpURLConnection conn = (HttpURLConnection) new URL("http://192.168.1.115:8080/dn_network_cache_server/MyServlet1").openConnection();
                    conn.setRequestMethod("GET");
                    conn.setDoInput(true);
                    conn.connect();
                    int responseCode = conn.getResponseCode();
                    if(responseCode==HttpStatus.SC_OK){
                        InputStream is = conn.getInputStream();
                        BufferedReader br = new BufferedReader(new InputStreamReader(is));
                        Log.d(TAG, br.readLine());
                    }else{
                        Log.d(TAG, responseCode+"");
                    }
                    
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    
    public void request2(View v){
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                try {
                    BitmapFactory.decodeStream((InputStream) new URL("http://192.168.1.115:8080/dn_network_cache_server/icon.png").getContent());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                
            }
        }).start();
    }
    
    public void deleteCache(View v){
        HttpResponseCache cache = HttpResponseCache.getInstalled();
        if(cache!=null){
            try {
                cache.delete();
                Log.d(TAG, "Çå¿Õ»º´æ");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        
    }
    

三级缓存
内存缓存、外部缓存
算法LruCache
+自定的 一周过期等等。
Sqlite缓存+加密

你可能感兴趣的:(网络优化(一)缓存优化)