• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

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

武飞扬头像
juejin
帮助60

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);
​

4.3.构造消息发送类(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;
}
}

4.发布消息

EventBus.getDefault().post(new Student("刘哈哈", 27));

5.接收消息:可以有四种线程模型选择

//接收事件
@Subscribe(threadMode = ThreadMode.MAIN)
public void studentEventBus(Student student){
mShow.setText("姓名:" student.getName() " " "年龄:" student.getAge());
}

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

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

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

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

文末

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

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

 

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanfbajh
系列文章
更多 icon
同类精品
更多 icon
继续加载