本文正在参与「金石方案 . 分割6万现金大奖」

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

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

如有其他问题能够点下方链接

传送直达↓↓↓docs.qq.com/doc/DUkNRVF…

文末

在网络传输中咱们一般都会敞开GZIP紧缩,可是出于寻根究底的天性只是知道如何敞开就不能满足俺的好奇心的,所以想着写个demo测试一下比较常用的两个数据紧缩方法,GZIP/ZIP紧缩。

GZIP是网站紧缩加快的一种技能,对于敞开后能够加快咱们网站的翻开速度,原理是通过服务器紧缩,客户端浏览器快速解压的原理,能够大大减少了网站的流量。

本文正在参与「金石方案 . 分割6万现金大奖」