Android性能优化之数据传输效率优化

一般地,数据传输大都是用数据的序列化和反序列化来完成的(即服务器对象Object----流—>客户端Object对象)
这样传输的过程中需要花费大量的时间来解析数据。这里介绍一个基于二进制文件来进行传输的方式——FlatBuffers。这种方式只需要把byte数据加载到内存中即可,不需要像json那样进行序列化与反序列化,大大节约了传输的时间。

FlatBuffers的使用方法:首先按照使用特定的 IDL 定义数据结构 schema,然后使用编译工具 flatc 编译 schema 生成对应的代码,把生成的代码应用到工程中即可。

代码展示如下

1.编写schema文件,文件名为“sample_schema.fbs”:

table PeopleList {
    peoples : [People];
}

table People {
    id : string;
    index : long;
    guid : string;
    name : string;
	gender : string;
	company : string;
	email : string;
	friends : [Friend];
}

table Friend {
    id : long;
    name : string;
}

root_type PeopleList;

2.使用 flatc 可以把 Schema 编译成多种编程语言,我们仅仅讨论 Android 平台,所以把 Schema 编译成 Java,找到flatc.exe执行命令如下:flatc –j -b sample_schema.fbs
执行完命令之后会生成三个java文件,将这三个java文件导入工程即可
在这里插入图片描述
MainActivity:

package com.flatbuffer;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.flatbuffer.flatmodel.PeopleList;
import com.flatbuffer.jsonmodel.PeopleListJson;
import com.flatbuffer.utils.Utils;
import com.google.gson.Gson;
import java.nio.ByteBuffer;

public class MainActivity extends AppCompatActivity {

    public static final String TAG = MainActivity.class.getSimpleName();
    private TextView textViewFlat, textViewJson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textViewFlat = (TextView) findViewById(R.id.textViewFlat);
        textViewJson = (TextView) findViewById(R.id.textViewJson);
    }

    public void loadFromFlatBuffer(View view) {
        byte[] buffer = Utils.readRawResource(getApplication(), R.raw.sample_flatbuffer);
        long startTime = System.currentTimeMillis();
        ByteBuffer bb = ByteBuffer.wrap(buffer);
        PeopleList peopleList = PeopleList.getRootAsPeopleList(bb);
        long timeTaken = System.currentTimeMillis() - startTime;
        String logText = "FlatBuffer : " + timeTaken + "ms";
        textViewFlat.setText(logText);
        Log.d(TAG, "loadFromFlatBuffer " + logText);
    }

    public void loadFromJson(View view) {
        String jsonText = new String(Utils.readRawResource(getApplication(), R.raw.sample_json));
        long startTime = System.currentTimeMillis();
        PeopleListJson peopleList = new Gson().fromJson(jsonText, PeopleListJson.class);
        long timeTaken = System.currentTimeMillis() - startTime;
        String logText = "Json : " + timeTaken + "ms";
        textViewJson.setText(logText);
        Log.d(TAG, "loadFromJson " + logText);
    }
}

Utils类:


package com.flatbuffer.utils;

import android.content.Context;

import java.io.IOException;
import java.io.InputStream;

public class Utils {

    public static byte[] readRawResource(Context context, int resId) {
        InputStream stream = null;
        byte[] buffer = null;
        try {
            stream = context.getResources().openRawResource(resId);
            buffer = new byte[stream.available()];
            while (stream.read(buffer) != -1) ;
        } catch (IOException e) {
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                }
            }
        }
        return buffer;
    }
}

工程文件结构:
在这里插入图片描述
jsonmodel:

package com.flatbuffer.jsonmodel;

import java.util.ArrayList;

public class PeopleListJson {
    public ArrayList<PeopleJson> peoples;
}
package com.flatbuffer.jsonmodel;

import java.util.ArrayList;

public class PeopleJson {
    public String id;
    public long index;
    public String guid;
    public String name;
    public String gender;
    public String company;
    public String email;
    public ArrayList<FriendJson> friends;

}

package com.flatbuffer.jsonmodel;

public class FriendJson {
    public long id;
    public String name;
}

运行结果:
在这里插入图片描述

发布了61 篇原创文章 · 获赞 0 · 访问量 885

猜你喜欢

转载自blog.csdn.net/qq_36828822/article/details/103755914