温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

android中gzip数据压缩与网络框架解压缩怎么实现

发布时间:2022-11-09 09:12:46 来源:亿速云 阅读:101 作者:iii 栏目:开发技术

这篇“android中gzip数据压缩与网络框架解压缩怎么实现”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“android中gzip数据压缩与网络框架解压缩怎么实现”文章吧。

    theme: smartblue

    gzip是一种常用的压缩算法,它是若干种文件压缩程序的简称,通常指GNU计划的实现,此处的gzip代表GNU zip。

    HTTP协议上的GZIP编码是一种用来改进WEB应用程序性能的技术。大流量的WEB站点常常使用GZIP压缩技术来让用户感受更快的速度。

    开GZIP有什么好处?

    Gzip开启以后会将输出到用户浏览器的数据进行压缩的处理,这样就会减小通过网络传输的数据量,提高浏览的速度。

    Java中gzip压缩和解压实现

    字节流压缩:

        /**
         * 字节流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;
        }

    字节流解压:

        /**
         * gzip解压
         * @param data
         * @return
         */
        public static byte[] unGZip(byte[] data){
            // 创建一个新的输出流
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            try {
                ByteArrayInputStream in = new ByteArrayInputStream(data);
                GZIPInputStream gzip = new GZIPInputStream(in);
                byte[] buffer = new byte[4096];
                int n = 0;
                // 将解压后的数据写入输出流
                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();
        }

    网络框架解压缩(gzip)

    一、采用内存数据库保存记录。

    二、请求时采用重新开新线程方式,在子线程中请求网络请求。

    三、数据请求后,可通过EventBus来设置返回结果的参数和返回信息,若其它类需要获取状态时,需要自己注册监听,动态去获取返回值。

    使用场景:应用程序内各组件间、组件与后台线程间的通信。

    比如请求网络,等网络返回时通过Handler或Broadcast通知UI,两个Fragment之间需要通过Listener通信,这些需求都可以通过EventBus实现。

    使用步骤:

    \1. 添加依赖:implementation 'org.greenrobot:eventbus:3.0.0'
    
    \2. 注册:EventBus.getDefault().register(this);
    构造消息发送类(post调用的对象)
    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;
    }
    }
    发布消息
    EventBus.getDefault().post(new Student("刘哈哈", 27));
    接收消息:可以有四种线程模型选择
    //接收事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void studentEventBus(Student student){
    mShow.setText("姓名:"+student.getName()+" "+"年龄:"+student.getAge());
    }

    解注册(防止内存泄漏):EventBus.getDefault().unregister(this);

    网络请求成功后,需要注意文件流的大小,太大容易下载缓慢,解决缓慢问题

    1、JSON返回格式,尽量去KEY,将JSONOBJECT修改为JSONArray格式。

    2、对数据进行压缩,采用GZIP对数据进行压缩处理:网络请求时服务器对数据压缩,移动端请求到结果后,再进行解压。

    以上就是关于“android中gzip数据压缩与网络框架解压缩怎么实现”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI