溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

android中g(shù)zip數(shù)據(jù)壓縮與網(wǎng)絡(luò)框架解壓縮怎么實現(xiàn)

發(fā)布時間:2022-11-09 09:12:46 來源:億速云 閱讀:113 作者:iii 欄目:開發(fā)技術(shù)

這篇“android中g(shù)zip數(shù)據(jù)壓縮與網(wǎng)絡(luò)框架解壓縮怎么實現(xiàn)”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“android中g(shù)zip數(shù)據(jù)壓縮與網(wǎng)絡(luò)框架解壓縮怎么實現(xiàn)”文章吧。

    theme: smartblue

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

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

    開GZIP有什么好處?

    Gzip開啟以后會將輸出到用戶瀏覽器的數(shù)據(jù)進(jìn)行壓縮的處理,這樣就會減小通過網(wǎng)絡(luò)傳輸?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)絡(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實現(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)行解壓。

    以上就是關(guān)于“android中g(shù)zip數(shù)據(jù)壓縮與網(wǎng)絡(luò)框架解壓縮怎么實現(xiàn)”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI