android中g(shù)zip數(shù)據(jù)壓縮與網(wǎng)絡框架解壓縮
theme: smartblue
gzip是一種常用的壓縮算法,它是若干種文件壓縮程序的簡稱,通常指GNU計劃的實現(xiàn),此處的gzip代表GNU zip。
HTTP協(xié)議上的GZIP編碼是一種用來改進WEB應用程序性能的技術(shù)。大流量的WEB站點常常使用GZIP壓縮技術(shù)來讓用戶感受更快的速度。
開GZIP有什么好處?
Gzip開啟以后會將輸出到用戶瀏覽器的數(shù)據(jù)進行壓縮的處理,這樣就會減小通過網(wǎng)絡傳輸?shù)臄?shù)據(jù)量,提高瀏覽的速度。
Java中g(shù)zip壓縮和解壓實現(xiàn)
字節(jié)流壓縮:
/** * 字節(jié)流gzip壓縮 * @param data * @return */ public static byte[] gZip(byte[] data) { byte[] b = null; try { ByteArrayInputStream in = new ByteArrayInputStream(data); ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(out); byte[] buffer = new byte[4096]; int n = 0; while((n = in.read(buffer, 0, buffer.length)) > 0){ gzip.write(buffer, 0, n); } gzip.close(); in.close(); b = out.toByteArray(); out.close(); } catch (Exception ex) { ex.printStackTrace(); } return b; }
字節(jié)流解壓:
/** * gzip解壓 * @param data * @return */ public static byte[] unGZip(byte[] data){ // 創(chuàng)建一個新的輸出流 ByteArrayOutputStream out = new ByteArrayOutputStream(); try { ByteArrayInputStream in = new ByteArrayInputStream(data); GZIPInputStream gzip = new GZIPInputStream(in); byte[] buffer = new byte[4096]; int n = 0; // 將解壓后的數(shù)據(jù)寫入輸出流 while ((n = gzip.read(buffer)) >= 0) { out.write(buffer, 0, n); } in.close(); gzip.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } return out.toByteArray(); }
網(wǎng)絡框架解壓縮(gzip)
一、采用內(nèi)存數(shù)據(jù)庫保存記錄。
二、請求時采用重新開新線程方式,在子線程中請求網(wǎng)絡請求。
三、數(shù)據(jù)請求后,可通過EventBus來設置返回結(jié)果的參數(shù)和返回信息,若其它類需要獲取狀態(tài)時,需要自己注冊監(jiān)聽,動態(tài)去獲取返回值。
使用場景:應用程序內(nèi)各組件間、組件與后臺線程間的通信。
比如請求網(wǎng)絡,等網(wǎng)絡返回時通過Handler或Broadcast通知UI,兩個Fragment之間需要通過Listener通信,這些需求都可以通過EventBus實現(xiàn)。
使用步驟:
? \1. 添加依賴:implementation 'org.greenrobot:eventbus:3.0.0' ? \2. 注冊:EventBus.getDefault().register(this); ?
構(gòu)造消息發(fā)送類(post調(diào)用的對象)
public class Student { private String name; private int age; ? public Student(String name, int age) { this.name = name; this.age = age; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public int getAge() { return age; } }
發(fā)布消息
EventBus.getDefault().post(new Student("劉哈哈", 27));
接收消息:可以有四種線程模型選擇
//接收事件 @Subscribe(threadMode = ThreadMode.MAIN) public void studentEventBus(Student student){ mShow.setText("姓名:"+student.getName()+" "+"年齡:"+student.getAge()); }
解注冊(防止內(nèi)存泄漏):EventBus.getDefault().unregister(this);
網(wǎng)絡請求成功后,需要注意文件流的大小,太大容易下載緩慢,解決緩慢問題
1、JSON返回格式,盡量去KEY,將JSONOBJECT修改為JSONArray格式。
2、對數(shù)據(jù)進行壓縮,采用GZIP對數(shù)據(jù)進行壓縮處理:網(wǎng)絡請求時服務器對數(shù)據(jù)壓縮,移動端請求到結(jié)果后,再進行解壓。
文末
在網(wǎng)絡傳輸中我們一般都會開啟GZIP壓縮,但是出于刨根問底的天性僅僅知道如何開啟就不能滿足俺的好奇心的,所以想著寫個demo測試一下比較常用的兩個數(shù)據(jù)壓縮方式,GZIP/ZIP壓縮。
GZIP是網(wǎng)站壓縮加速的一種技術(shù),對于開啟后可以加快我們網(wǎng)站的打開速度,原理是經(jīng)過服務器壓縮,客戶端瀏覽器快速解壓的原理,可以大大減少了網(wǎng)站的流量。
以上就是android中g(shù)zip數(shù)據(jù)壓縮與網(wǎng)絡框架解壓縮的詳細內(nèi)容,更多關(guān)于android gzip數(shù)據(jù)壓縮解壓縮的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android 獲取屏幕高度,標題高度,狀態(tài)欄高度(實例代碼)
getWindow().findViewById(Window.ID_ANDROID_CONTENT)這個方法獲取到的view就是程序不包括標題欄的部分,然后就可以知道標題欄的高度了2013-11-11android檢測網(wǎng)絡連接狀態(tài)示例講解
網(wǎng)絡的時候,并不是每次都能連接到網(wǎng)絡,因此在程序啟動中需要對網(wǎng)絡的狀態(tài)進行判斷,如果沒有網(wǎng)絡則提醒用戶進行設置2014-02-02Android中ViewPager你所不知道的優(yōu)化技巧分享
提到ViewPager想必各位同學一點都不陌生,它是Android中最常用的組件之一,這篇文章小編就帶大家一起來看看ViewPager一些新的優(yōu)化方式吧2024-04-04Android中findViewById獲取控件返回為空問題怎么解決
這篇文章主要介紹了Android中findViewById獲取控件返回為空問題怎么解決的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-06-06