Open a title article
Recently, the company is using protocol buffer instead of the original JSON as the data Interchange format of the mobile side. Although the service and CTO of the new technology blowing the hype, said what small size, not easy to crack the mess. But as a person on the Android side, I just care about how the data is resolved. Toss for two days, finally analytic success, I come to share my process, what better advice everyone to mention.
Generate Java Entity class Chapter
Online find a lot of raiders. Are talking about the download and compilation of protocol, most of the links have not been directly downloaded. I'll just say what I do.
Download the latest Proctoc.exe link address https://github.com/google/protobuf/releases ">https://github.com/google/protobuf/releases
There is a protoc-3.0.0-alpha-3-win32.zip direct download. To get protoc.exe in there.
Configure to environment variables, this is a veteran. It is easy to use the black form anywhere in the system.
Get the server-side supply of his defined. Proto file
After compiling the. proto file protoc --java_out=D:/ test.proto Javaout is the path address of the compiled output, and the second parameter is the proto file that needs to be compiled.
After the compilation succeeds, the generated JavaBean are copied directly to the Android project.
Parsing tool Articles
Once connected to the project, the network access layer is used in the xutils framework of the httputils, which has the advantages and disadvantages, as if the people themselves encapsulated, in the method of successful request to directly convert the results into a string, I've been experimenting with this for a very long time. The android-async-http link https://github.com/loopj/android-async-http directly returns a byte stream, using byte stream parsing. (PS is not lazy to write the framework, the program of the ape Word: Do not repeatedly build wheels.) Besides, the project manager can't give you time to write an almost perfect framework for yourself.
So the parsing tool is ready.
Project Dependent Library Chapter
Depending on the library, because Protocolbuffer is provided by Google, using Androidstudio is very easy to introduce the latest dependent libraries. Directly select the item by pressing F4, select the Dependencies tab, join the dependent library from the network, enter KEYWORDCOM.GOOGLE.PROTOBUF to be able to search for the latest Protocolbuffer dependent library. (The Eclipse developer simply drags the studio-downloaded jar in after the introduction of studio)
Analytic Chapter
After the parsing tools, dependent libraries, and entity classes are all imported into the project. Can be based on the interface provided by the server after the success of the analysis
Very simple parsing, my entity class is called API. Analysis on a sentence
API API = Api.parsefrom (byte[] data)
Language is not good. Just so much to say, there is a direct comment that needs my help.
Summarize
Protocolbuffer's strengths I made clear that small size is not easy to be cracked, other feelings nothing, as a mobile developer, in fact, the concern is the assumption of parsing this thing.
Protocolbuffer on the Android side