最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android中g(shù)zip數(shù)據(jù)壓縮與網(wǎng)絡(luò)框架解壓縮

 更新時(shí)間:2022年11月08日 10:08:19   作者:Coolbreeze  
這篇文章主要為大家介紹了android中g(shù)zip數(shù)據(jù)壓縮與網(wǎng)絡(luò)框架解壓縮實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

theme: smartblue

gzip是一種常用的壓縮算法,它是若干種文件壓縮程序的簡(jiǎn)稱,通常指GNU計(jì)劃的實(shí)現(xiàn),此處的gzip代表GNU zip。

HTTP協(xié)議上的GZIP編碼是一種用來改進(jìn)WEB應(yīng)用程序性能的技術(shù)。大流量的WEB站點(diǎn)常常使用GZIP壓縮技術(shù)來讓用戶感受更快的速度。

開GZIP有什么好處?

Gzip開啟以后會(huì)將輸出到用戶瀏覽器的數(shù)據(jù)進(jìn)行壓縮的處理,這樣就會(huì)減小通過網(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)建一個(gè)新的輸出流
        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ù)庫保存記錄。

二、請(qǐng)求時(shí)采用重新開新線程方式,在子線程中請(qǐng)求網(wǎng)絡(luò)請(qǐng)求。

三、數(shù)據(jù)請(qǐng)求后,可通過EventBus來設(shè)置返回結(jié)果的參數(shù)和返回信息,若其它類需要獲取狀態(tài)時(shí),需要自己注冊(cè)監(jiān)聽,動(dòng)態(tài)去獲取返回值。

使用場(chǎng)景:應(yīng)用程序內(nèi)各組件間、組件與后臺(tái)線程間的通信。

比如請(qǐng)求網(wǎng)絡(luò),等網(wǎng)絡(luò)返回時(shí)通過Handler或Broadcast通知UI,兩個(gè)Fragment之間需要通過Listener通信,這些需求都可以通過EventBus實(shí)現(xiàn)。

使用步驟:

?
\1. 添加依賴:implementation 'org.greenrobot:eventbus:3.0.0'
?
\2. 注冊(cè):EventBus.getDefault().register(this);
?

構(gòu)造消息發(fā)送類(post調(diào)用的對(duì)象)

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());
}

解注冊(cè)(防止內(nèi)存泄漏):EventBus.getDefault().unregister(this);

網(wǎng)絡(luò)請(qǐng)求成功后,需要注意文件流的大小,太大容易下載緩慢,解決緩慢問題

1、JSON返回格式,盡量去KEY,將JSONOBJECT修改為JSONArray格式。

2、對(duì)數(shù)據(jù)進(jìn)行壓縮,采用GZIP對(duì)數(shù)據(jù)進(jìn)行壓縮處理:網(wǎng)絡(luò)請(qǐng)求時(shí)服務(wù)器對(duì)數(shù)據(jù)壓縮,移動(dòng)端請(qǐng)求到結(jié)果后,再進(jìn)行解壓。

文末

在網(wǎng)絡(luò)傳輸中我們一般都會(huì)開啟GZIP壓縮,但是出于刨根問底的天性僅僅知道如何開啟就不能滿足俺的好奇心的,所以想著寫個(gè)demo測(cè)試一下比較常用的兩個(gè)數(shù)據(jù)壓縮方式,GZIP/ZIP壓縮。

GZIP是網(wǎng)站壓縮加速的一種技術(shù),對(duì)于開啟后可以加快我們網(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ù)壓縮解壓縮的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

西贡区| 安溪县| 博野县| 荥阳市| 塘沽区| 聂拉木县| 西宁市| 松江区| 华容县| 南安市| 界首市| 嘉荫县| 大冶市| 衡阳县| 德化县| 台北县| 古丈县| 临武县| 营口市| 申扎县| 韶关市| 彩票| 大理市| 宣城市| 金昌市| 高唐县| 仙居县| 上林县| 沁水县| 商南县| 淳安县| 志丹县| 盖州市| 汝南县| 山西省| 合作市| 安化县| 九龙县| 通道| 内黄县| 农安县|