广告
返回顶部
首页 > 资讯 > 移动开发 >android中gzip数据压缩与网络框架解压缩
  • 605
分享到

android中gzip数据压缩与网络框架解压缩

android gzip数据压缩解压缩android gzip 网络框架解压 2022-11-13 19:11:46 605人浏览 泡泡鱼
摘要

目录theme: smartblue开GZIP有什么好处?Java中gzip压缩和解压实现字节流压缩:字节流解压:网络框架解压缩(gzip)使用步骤:构造消息发送类(post调用的对

theme: smartblue

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

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

开GZIP有什么好处?

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

Java中gzip压缩和解压实现

字节流压缩:

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

字节流解压:

    
    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对数据进行压缩处理:网络请求时服务器对数据压缩,移动端请求到结果后,再进行解压。

文末

在网络传输中我们一般都会开启GZIP压缩,但是出于刨根问底的天性仅仅知道如何开启就不能满足俺的好奇心的,所以想着写个demo测试一下比较常用的两个数据压缩方式,GZIP/ZIP压缩。

GZIP是网站压缩加速的一种技术,对于开启后可以加快我们网站的打开速度,原理是经过服务器压缩,客户端浏览器快速解压的原理,可以大大减少了网站的流量。

以上就是Android中gzip数据压缩与网络框架解压缩的详细内容,更多关于android gzip数据压缩解压缩的资料请关注编程网其它相关文章!

--结束END--

本文标题: android中gzip数据压缩与网络框架解压缩

本文链接: https://www.lsjlt.com/news/170989.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作