android中g(shù)zip數(shù)據(jù)壓縮與網(wǎng)絡(luò)框架解壓縮
theme: smartblue
gzip是一種常用的壓縮算法,它是若干種文件壓縮程序的簡稱,通常指GNU計劃的實(shí)現(xiàn),此處的gzip代表GNU zip。
HTTP協(xié)議上的GZIP編碼是一種用來改進(jìn)WEB應(yīng)用程序性能的技術(shù)。大流量的WEB站點(diǎn)常常使用GZIP壓縮技術(shù)來讓用戶感受更快的速度。
開GZIP有什么好處?
Gzip開啟以后會將輸出到用戶瀏覽器的數(shù)據(jù)進(jìn)行壓縮的處理,這樣就會減小通過網(wǎng)絡(luò)傳輸?shù)臄?shù)據(jù)量,提高瀏覽的速度。
Java中g(shù)zip壓縮和解壓實(shí)現(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)絡(luò)框架解壓縮(gzip)
一、采用內(nèi)存數(shù)據(jù)庫保存記錄。
二、請求時采用重新開新線程方式,在子線程中請求網(wǎng)絡(luò)請求。
三、數(shù)據(jù)請求后,可通過EventBus來設(shè)置返回結(jié)果的參數(shù)和返回信息,若其它類需要獲取狀態(tài)時,需要自己注冊監(jiān)聽,動態(tài)去獲取返回值。
使用場景:應(yīng)用程序內(nèi)各組件間、組件與后臺線程間的通信。
比如請求網(wǎng)絡(luò),等網(wǎng)絡(luò)返回時通過Handler或Broadcast通知UI,兩個Fragment之間需要通過Listener通信,這些需求都可以通過EventBus實(shí)現(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)絡(luò)請求成功后,需要注意文件流的大小,太大容易下載緩慢,解決緩慢問題
1、JSON返回格式,盡量去KEY,將JSONOBJECT修改為JSONArray格式。
2、對數(shù)據(jù)進(jìn)行壓縮,采用GZIP對數(shù)據(jù)進(jìn)行壓縮處理:網(wǎng)絡(luò)請求時服務(wù)器對數(shù)據(jù)壓縮,移動端請求到結(jié)果后,再進(jìn)行解壓。

文末
在網(wǎng)絡(luò)傳輸中我們一般都會開啟GZIP壓縮,但是出于刨根問底的天性僅僅知道如何開啟就不能滿足俺的好奇心的,所以想著寫個demo測試一下比較常用的兩個數(shù)據(jù)壓縮方式,GZIP/ZIP壓縮。
GZIP是網(wǎng)站壓縮加速的一種技術(shù),對于開啟后可以加快我們網(wǎng)站的打開速度,原理是經(jīng)過服務(wù)器壓縮,客戶端瀏覽器快速解壓的原理,可以大大減少了網(wǎng)站的流量。
以上就是android中g(shù)zip數(shù)據(jù)壓縮與網(wǎng)絡(luò)框架解壓縮的詳細(xì)內(nèi)容,更多關(guān)于android gzip數(shù)據(jù)壓縮解壓縮的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android開發(fā)之機(jī)頂盒上gridview和ScrollView的使用詳解
這篇文章主要介紹了Android開發(fā)之機(jī)頂盒上gridview和ScrollView的使用詳解的相關(guān)資料,需要的朋友可以參考下2016-02-02
Android實(shí)現(xiàn)垂直進(jìn)度條VerticalSeekBar
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)垂直進(jìn)度條VerticalSeekBar的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07
Android實(shí)現(xiàn)帶有指示器的自定義底部導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶有指示器的自定義底部導(dǎo)航欄,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
使用Android自定義控件實(shí)現(xiàn)滑動解鎖九宮格
最近由于Android項目需要,要求做一個類似于支付寶的九宮格解鎖組件,下面小編給大家分享了具體實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-10-10
安卓(Android)實(shí)現(xiàn)3DTouch效果
3DTouch是什么效果的大家應(yīng)該都知道了。本文將介紹在Android中如何實(shí)現(xiàn)3DTouch的效果,有需要的可以參考學(xué)習(xí)。2016-08-08
Android編程獲取設(shè)備MAC地址的實(shí)現(xiàn)方法
這篇文章主要介紹了Android編程獲取設(shè)備MAC地址的實(shí)現(xiàn)方法,涉及Android針對硬件設(shè)備的操作技巧,需要的朋友可以參考下2017-01-01

