Protocolbuffer on the Android side

Source: Internet
Author: User

Open a title article

Recently, the company is using protocol buffer instead of the original JSON as the mobile data Interchange format, although the service and CTO blew the new technology, said what small size, not easy to break the mess, but as the Android personnel, I only care about the problem of how the data is parsed. Toss for two days, the final analysis of 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 protocol download and compilation, to the most of the links have not been directly downloaded, I directly say how 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 directly download, you can get protoc.exe in it
Configuration to environment variables, this is a veteran, in the system under its own configuration can be used anywhere in the black window.
Get the server-side supply of his well-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

Before the project where the network access layer uses the Httputils in the Xutils framework, this has the advantages and disadvantages, as if the people themselves encapsulated, in the method of successful request to directly convert the result into a string, I've been experimenting with this for a 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 programmer of the word: Do not repeat the wheel. 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, it is easy to use Androidstudio to introduce the latest dependent libraries, directly select items by pressing F4, select the Dependencies tab, add dependent libraries from the network, Enter the keyword COM.GOOGLE.PROTOBUF to search for the latest Protocolbuffer dependent libraries. (The Eclipse developer simply drags the studio-downloaded jar in after studio introduction)

Analytic Chapter

Once the parsing tools, dependent libraries, and entity classes are all imported into the project, they can be resolved based on the successful interface provided by the server.
Very simple parsing, my entity class is called API, parsing a sentence
API API = Api.parsefrom (byte[] data)
Language is not good, can only say so much, there is a need for my help direct comment on it.

Summarize

The advantages of Protocolbuffer I understand the small size is not easy to be cracked, the other feeling nothing, as a mobile developer, in fact, the concern is still if you parse this thing.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Protocolbuffer on the Android side

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.