手机端实现Protocol Buffer

来源:互联网 发布:儿童英语网络课程 编辑:程序博客网 时间:2024/05/29 18:06

最近在研究push技术,里面涉及到了Protocol Buffer来进行数据的封装,在网上查了一些资料,写个帖子记录一下。


简介:

protocol buffer 是 google 的一种数据交换的格式,它独立于语言,独立于平台。google 提供了三种语言的实现:java、c++ 和 python,每一种实现都包含了相应语言的编译器以及库文件。由于它是一种二进制的格式,比使用 xml 进行数据交换快许多。可以把它用于分布式应用之间的数据通信或者异构环境下的数据交换。


安装和使用:

具体的安装和简单测试,在网上有很多,下面这个网址是我认为比较好的,也是进行了参考的。现在贡献给大家,希望大家能学到东西。

http://lichen.blog.51cto.com/697816/284015

里面详细介绍了如何下载Protocol Buffer、编译打包,并且用Java语言实现了自带的一个示例,很简单明了易懂。

下面这个网址,http://dangfan.me/2012/02/04/protocol-buffers/,简单介绍了使用Protocol Buffer时的代码书写的信息,设计部分API,使用Python语言实现了一个电话本的功能。由于Python本人不懂,所以也只是简单的看了一下,并没有太深入的研究,有会Python的可以去看看。


下面再奉献两个好的网址,一个是Google文档,一个是API文档

Protocol Buffer的Google文档:

https://developers.google.com/protocol-buffers/docs/proto?hl=zh-CN

Protocol Buffer的API文档:

https://developers.google.com/protocol-buffers/docs/reference/python/google.protobuf.message.Message-class?hl=zh-CN


好,介绍了一下Protocol Buffer(一下简称PB吧,写着太累了),下面开始我的Android实现。


先上一张图:



主要完成一个很简单的功能,在三个EditText中分别输入学号、姓名、性别,点击“提交”后保存在SDcard根目录下的一个叫做“roster.dat”文件中,然后再通过解析该文件列出所有保存过的内容。


下面介绍实现的步骤:

步骤一、书写 .proto 文件内容。

[plain] view plaincopy
  1. // This is a file in Proto format. Defines a Roster data-structure.  
  2. // It's my first .proto file, if you have something wanna know, please contact me.  
  3. // EMAIL: brokenrings@163.com  
  4. // CSDN_BLOG: http://my.csdn.net/carterjin  
  5.   
  6.   
  7.   
  8. // The following two lines defines the package and classname field.  
  9. // When using protoc.exe compiling it, compilier will create a Java file using the given package structure, and named Java file  
  10. // using the given classname.  
  11. // Ex. com.carter.roster.proto.Roster.java  
  12. option java_package = "com.carter.roster.proto";  
  13. option java_outer_classname = "Roster";  
  14.   
  15.   
  16. message Student{  
  17.     required int32 id = 1;  
  18.     required string name = 2;  
  19.       
  20.     enum Sex{  
  21.         MALE = 0;  
  22.         FEMALE = 1;  
  23.     }  
  24.       
  25.     required Sex sex = 3;  
  26.   
  27. }  
  28.   
  29.   
  30.   
  31. message StudentRoster{  
  32.     repeated Student student = 1;  
  33. }  

上面是.proto文件的内容,头两行(不算注释)定义了要编译成Java文件后的包名和类名,proto编译器会自动按照给定的名称生成路径和文件,例如当编译后会有“com.carter.roster.proto.Roster.java”。


步骤二、新建一个Android项目

我把步骤一写好的proto文件直接放置在了src目录下,因为这样我就可以直接进入该目录编译生成一个标准的包目录格式了。

通过cmd(或例如Total Commander等)进入到src目录下,执行"protoc --java_out=. roster.proto",会在src的com/carter/roster/proto目录下生成Roster.java。这个Java文件就是protoc通过解析proto文件自动生成的。里面的东西不用动,到时在项目里直接调用就好了。

下面粘出来出来我的生成的文件:有精力的可以看看

[java] view plaincopy
  1. // Generated by the protocol buffer compiler. DO NOT EDIT!  
  2. // source: roster.proto  
  3.   
  4. package com.carter.roster.proto;  
  5.   
  6. public final class Roster {  
  7.     private Roster() {  
  8.     }  
  9.   
  10.     public static void registerAllExtensions(com.google.protobuf.ExtensionRegistry registry) {  
  11.     }  
  12.   
  13.     public static final class Student extends com.google.protobuf.GeneratedMessage {  
  14.         // Use Student.newBuilder() to construct.  
  15.         private Student() {  
  16.             initFields();  
  17.         }  
  18.   
  19.         private Student(boolean noInit) {  
  20.         }  
  21.   
  22.         private static final Student defaultInstance;  
  23.   
  24.         public static Student getDefaultInstance() {  
  25.             return defaultInstance;  
  26.         }  
  27.   
  28.         public Student getDefaultInstanceForType() {  
  29.             return defaultInstance;  
  30.         }  
  31.   
  32.         public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() {  
  33.             return com.carter.roster.proto.Roster.internal_static_Student_descriptor;  
  34.         }  
  35.   
  36.         protected com.google.protobuf.GeneratedMessage.FieldAccessorTable internalGetFieldAccessorTable() {  
  37.             return com.carter.roster.proto.Roster.internal_static_Student_fieldAccessorTable;  
  38.         }  
  39.   
  40.         public enum Sex implements com.google.protobuf.ProtocolMessageEnum {  
  41.             MALE(00), FEMALE(11), ;  
  42.   
  43.             public final int getNumber() {  
  44.                 return value;  
  45.             }  
  46.   
  47.             public static Sex valueOf(int value) {  
  48.                 switch (value) {  
  49.                     case 0:  
  50.                         return MALE;  
  51.                     case 1:  
  52.                         return FEMALE;  
  53.                     default:  
  54.                         return null;  
  55.                 }  
  56.             }  
  57.   
  58.             public static com.google.protobuf.Internal.EnumLiteMap<Sex> internalGetValueMap() {  
  59.                 return internalValueMap;  
  60.             }  
  61.   
  62.             private static com.google.protobuf.Internal.EnumLiteMap<Sex> internalValueMap = new com.google.protobuf.Internal.EnumLiteMap<Sex>() {  
  63.                 public Sex findValueByNumber(int number) {  
  64.                     return Sex.valueOf(number);  
  65.                 }  
  66.             };  
  67.   
  68.             public final com.google.protobuf.Descriptors.EnumValueDescriptor getValueDescriptor() {  
  69.                 return getDescriptor().getValues().get(index);  
  70.             }  
  71.   
  72.             public final com.google.protobuf.Descriptors.EnumDescriptor getDescriptorForType() {  
  73.                 return getDescriptor();  
  74.             }  
  75.   
  76.             public static final com.google.protobuf.Descriptors.EnumDescriptor getDescriptor() {  
  77.                 return com.carter.roster.proto.Roster.Student.getDescriptor().getEnumTypes().get(0);  
  78.             }  
  79.   
  80.             private static final Sex[] VALUES = { MALE, FEMALE, };  
  81.   
  82.             public static Sex valueOf(com.google.protobuf.Descriptors.EnumValueDescriptor desc) {  
  83.                 if (desc.getType() != getDescriptor()) {  
  84.                     throw new java.lang.IllegalArgumentException("EnumValueDescriptor is not for this type.");  
  85.                 }  
  86.                 return VALUES[desc.getIndex()];  
  87.             }  
  88.   
  89.             private final int index;  
  90.             private final int value;  
  91.   
  92.             private Sex(int index, int value) {  
  93.                 this.index = index;  
  94.                 this.value = value;  
  95.             }  
  96.   
  97.             static {  
  98.                 com.carter.roster.proto.Roster.getDescriptor();  
  99.             }  
  100.   
  101.             // @@protoc_insertion_point(enum_scope:Student.Sex)  
  102.         }  
  103.   
  104.         // required int32 id = 1;  
  105.         public static final int ID_FIELD_NUMBER = 1;  
  106.         private boolean hasId;  
  107.         private int id_ = 0;  
  108.   
  109.         public boolean hasId() {  
  110.             return hasId;  
  111.         }  
  112.   
  113.         public int getId() {  
  114.             return id_;  
  115.         }  
  116.   
  117.         // required string name = 2;  
  118.         public static final int NAME_FIELD_NUMBER = 2;  
  119.         private boolean hasName;  
  120.         private java.lang.String name_ = "";  
  121.   
  122.         public boolean hasName() {  
  123.             return hasName;  
  124.         }  
  125.   
  126.         public java.lang.String getName() {  
  127.             return name_;  
  128.         }  
  129.   
  130.         // required .Student.Sex sex = 3;  
  131.         public static final int SEX_FIELD_NUMBER = 3;  
  132.         private boolean hasSex;  
  133.         private com.carter.roster.proto.Roster.Student.Sex sex_;  
  134.   
  135.         public boolean hasSex() {  
  136.             return hasSex;  
  137.         }  
  138.   
  139.         public com.carter.roster.proto.Roster.Student.Sex getSex() {  
  140.             return sex_;  
  141.         }  
  142.   
  143.         private void initFields() {  
  144.             sex_ = com.carter.roster.proto.Roster.Student.Sex.MALE;  
  145.         }  
  146.   
  147.         public final boolean isInitialized() {  
  148.             if (!hasId)  
  149.                 return false;  
  150.             if (!hasName)  
  151.                 return false;  
  152.             if (!hasSex)  
  153.                 return false;  
  154.             return true;  
  155.         }  
  156.   
  157.         public void writeTo(com.google.protobuf.CodedOutputStream output) throws java.io.IOException {  
  158.             getSerializedSize();  
  159.             if (hasId()) {  
  160.                 output.writeInt32(1, getId());  
  161.             }  
  162.             if (hasName()) {  
  163.                 output.writeString(2, getName());  
  164.             }  
  165.             if (hasSex()) {  
  166.                 output.writeEnum(3, getSex().getNumber());  
  167.             }  
  168.             getUnknownFields().writeTo(output);  
  169.         }  
  170.   
  171.         private int memoizedSerializedSize = -1;  
  172.   
  173.         public int getSerializedSize() {  
  174.             int size = memoizedSerializedSize;  
  175.             if (size != -1)  
  176.                 return size;  
  177.   
  178.             size = 0;  
  179.             if (hasId()) {  
  180.                 size += com.google.protobuf.CodedOutputStream.computeInt32Size(1, getId());  
  181.             }  
  182.             if (hasName()) {  
  183.                 size += com.google.protobuf.CodedOutputStream.computeStringSize(2, getName());  
  184.             }  
  185.             if (hasSex()) {  
  186.                 size += com.google.protobuf.CodedOutputStream.computeEnumSize(3, getSex().getNumber());  
  187.             }  
  188.             size += getUnknownFields().getSerializedSize();  
  189.             memoizedSerializedSize = size;  
  190.             return size;  
  191.         }  
  192.   
  193.         public static com.carter.roster.proto.Roster.Student parseFrom(com.google.protobuf.ByteString data)  
  194.                 throws com.google.protobuf.InvalidProtocolBufferException {  
  195.             return newBuilder().mergeFrom(data).buildParsed();  
  196.         }  
  197.   
  198.         public static com.carter.roster.proto.Roster.Student parseFrom(com.google.protobuf.ByteString data,  
  199.                 com.google.protobuf.ExtensionRegistryLite extensionRegistry)  
  200.                 throws com.google.protobuf.InvalidProtocolBufferException {  
  201.             return newBuilder().mergeFrom(data, extensionRegistry).buildParsed();  
  202.         }  
  203.   
  204.         public static com.carter.roster.proto.Roster.Student parseFrom(byte[] data)  
  205.                 throws com.google.protobuf.InvalidProtocolBufferException {  
  206.             return newBuilder().mergeFrom(data).buildParsed();  
  207.         }  
  208.   
  209.         public static com.carter.roster.proto.Roster.Student parseFrom(byte[] data,  
  210.                 com.google.protobuf.ExtensionRegistryLite extensionRegistry)  
  211.                 throws com.google.protobuf.InvalidProtocolBufferException {  
  212.             return newBuilder().mergeFrom(data, extensionRegistry).buildParsed();  
  213.         }  
  214.   
  215.         public static com.carter.roster.proto.Roster.Student parseFrom(java.io.InputStream input)  
  216.                 throws java.io.IOException {  
  217.             return newBuilder().mergeFrom(input).buildParsed();  
  218.         }  
  219.   
  220.         public static com.carter.roster.proto.Roster.Student parseFrom(java.io.InputStream input,  
  221.                 com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException {  
  222.             return newBuilder().mergeFrom(input, extensionRegistry).buildParsed();  
  223.         }  
  224.   
  225.         public static com.carter.roster.proto.Roster.Student parseDelimitedFrom(java.io.InputStream input)  
  226.                 throws java.io.IOException {  
  227.             Builder builder = newBuilder();  
  228.             if (builder.mergeDelimitedFrom(input)) {  
  229.                 return builder.buildParsed();  
  230.             } else {  
  231.                 return null;  
  232.             }  
  233.         }  
  234.   
  235.         public static com.carter.roster.proto.Roster.Student parseDelimitedFrom(java.io.InputStream input,  
  236.                 com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException {  
  237.             Builder builder = newBuilder();  
  238.             if (builder.mergeDelimitedFrom(input, extensionRegistry)) {  
  239.                 return builder.buildParsed();  
  240.             } else {  
  241.                 return null;  
  242.             }  
  243.         }  
  244.   
  245.         public static com.carter.roster.proto.Roster.Student parseFrom(com.google.protobuf.CodedInputStream input)  
  246.                 throws java.io.IOException {  
  247.             return newBuilder().mergeFrom(input).buildParsed();  
  248.         }  
  249.   
  250.         public static com.carter.roster.proto.Roster.Student parseFrom(com.google.protobuf.CodedInputStream input,  
  251.                 com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException {  
  252.             return newBuilder().mergeFrom(input, extensionRegistry).buildParsed();  
  253.         }  
  254.   
  255.         public static Builder newBuilder() {  
  256.             return Builder.create();  
  257.         }  
  258.   
  259.         public Builder newBuilderForType() {  
  260.             return newBuilder();  
  261.         }  
  262.   
  263.         public static Builder newBuilder(com.carter.roster.proto.Roster.Student prototype) {  
  264.             return newBuilder().mergeFrom(prototype);  
  265.         }  
  266.   
  267.         public Builder toBuilder() {  
  268.             return newBuilder(this);  
  269.         }  
  270.   
  271.         public static final class Builder extends com.google.protobuf.GeneratedMessage.Builder<Builder> {  
  272.             private com.carter.roster.proto.Roster.Student result;  
  273.   
  274.             // Construct using com.carter.roster.proto.Roster.Student.newBuilder()  
  275.             private Builder() {  
  276.             }  
  277.   
  278.             private static Builder create() {  
  279.                 Builder builder = new Builder();  
  280.                 builder.result = new com.carter.roster.proto.Roster.Student();  
  281.                 return builder;  
  282.             }  
  283.   
  284.             protected com.carter.roster.proto.Roster.Student internalGetResult() {  
  285.                 return result;  
  286.             }  
  287.   
  288.             public Builder clear() {  
  289.                 if (result == null) {  
  290.                     throw new IllegalStateException("Cannot call clear() after build().");  
  291.                 }  
  292.                 result = new com.carter.roster.proto.Roster.Student();  
  293.                 return this;  
  294.             }  
  295.   
  296.             public Builder clone() {  
  297.                 return create().mergeFrom(result);  
  298.             }  
  299.   
  300.             public com.google.protobuf.Descriptors.Descriptor getDescriptorForType() {  
  301.                 return com.carter.roster.proto.Roster.Student.getDescriptor();  
  302.             }  
  303.   
  304.             public com.carter.roster.proto.Roster.Student getDefaultInstanceForType() {  
  305.                 return com.carter.roster.proto.Roster.Student.getDefaultInstance();  
  306.             }  
  307.   
  308.             public boolean isInitialized() {  
  309.                 return result.isInitialized();  
  310.             }  
  311.   
  312.             public com.carter.roster.proto.Roster.Student build() {  
  313.                 if (result != null && !isInitialized()) {  
  314.                     throw newUninitializedMessageException(result);  
  315.                 }  
  316.                 return buildPartial();  
  317.             }  
  318.   
  319.             private com.carter.roster.proto.Roster.Student buildParsed()  
  320.                     throws com.google.protobuf.InvalidProtocolBufferException {  
  321.                 if (!isInitialized()) {  
  322.                     throw newUninitializedMessageException(result).asInvalidProtocolBufferException();  
  323.                 }  
  324.                 return buildPartial();  
  325.             }  
  326.   
  327.             public com.carter.roster.proto.Roster.Student buildPartial() {  
  328.                 if (result == null) {  
  329.                     throw new IllegalStateException("build() has already been called on this Builder.");  
  330.                 }  
  331.                 com.carter.roster.proto.Roster.Student returnMe = result;  
  332.                 result = null;  
  333.                 return returnMe;  
  334.             }  
  335.   
  336.             public Builder mergeFrom(com.google.protobuf.Message other) {  
  337.                 if (other instanceof com.carter.roster.proto.Roster.Student) {  
  338.                     return mergeFrom((com.carter.roster.proto.Roster.Student) other);  
  339.                 } else {  
  340.                     super.mergeFrom(other);  
  341.                     return this;  
  342.                 }  
  343.             }  
  344.   
  345.             public Builder mergeFrom(com.carter.roster.proto.Roster.Student other) {  
  346.                 if (other == com.carter.roster.proto.Roster.Student.getDefaultInstance())  
  347.                     return this;  
  348.                 if (other.hasId()) {  
  349.                     setId(other.getId());  
  350.                 }  
  351.                 if (other.hasName()) {  
  352.                     setName(other.getName());  
  353.                 }  
  354.                 if (other.hasSex()) {  
  355.                     setSex(other.getSex());  
  356.                 }  
  357.                 this.mergeUnknownFields(other.getUnknownFields());  
  358.                 return this;  
  359.             }  
  360.   
  361.             public Builder mergeFrom(com.google.protobuf.CodedInputStream input,  
  362.                     com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException {  
  363.                 com.google.protobuf.UnknownFieldSet.Builder unknownFields = com.google.protobuf.UnknownFieldSet  
  364.                         .newBuilder(this.getUnknownFields());  
  365.                 while (true) {  
  366.                     int tag = input.readTag();  
  367.                     switch (tag) {  
  368.                         case 0:  
  369.                             this.setUnknownFields(unknownFields.build());  
  370.                             return this;  
  371.                         default: {  
  372.                             if (!parseUnknownField(input, unknownFields, extensionRegistry, tag)) {  
  373.                                 this.setUnknownFields(unknownFields.build());  
  374.                                 return this;  
  375.                             }  
  376.                             break;  
  377.                         }  
  378.                         case 8: {  
  379.                             setId(input.readInt32());  
  380.                             break;  
  381.                         }  
  382.                         case 18: {  
  383.                             setName(input.readString());  
  384.                             break;  
  385.                         }  
  386.                         case 24: {  
  387.                             int rawValue = input.readEnum();  
  388.                             com.carter.roster.proto.Roster.Student.Sex value = com.carter.roster.proto.Roster.Student.Sex  
  389.                                     .valueOf(rawValue);  
  390.                             if (value == null) {  
  391.                                 unknownFields.mergeVarintField(3, rawValue);  
  392.                             } else {  
  393.                                 setSex(value);  
  394.                             }  
  395.                             break;  
  396.                         }  
  397.                     }  
  398.                 }  
  399.             }  
  400.   
  401.             // required int32 id = 1;  
  402.             public boolean hasId() {  
  403.                 return result.hasId();  
  404.             }  
  405.   
  406.             public int getId() {  
  407.                 return result.getId();  
  408.             }  
  409.   
  410.             public Builder setId(int value) {  
  411.                 result.hasId = true;  
  412.                 result.id_ = value;  
  413.                 return this;  
  414.             }  
  415.   
  416.             public Builder clearId() {  
  417.                 result.hasId = false;  
  418.                 result.id_ = 0;  
  419.                 return this;  
  420.             }  
  421.   
  422.             // required string name = 2;  
  423.             public boolean hasName() {  
  424.                 return result.hasName();  
  425.             }  
  426.   
  427.             public java.lang.String getName() {  
  428.                 return result.getName();  
  429.             }  
  430.   
  431.             public Builder setName(java.lang.String value) {  
  432.                 if (value == null) {  
  433.                     throw new NullPointerException();  
  434.                 }  
  435.                 result.hasName = true;  
  436.                 result.name_ = value;  
  437.                 return this;  
  438.             }  
  439.   
  440.             public Builder clearName() {  
  441.                 result.hasName = false;  
  442.                 result.name_ = getDefaultInstance().getName();  
  443.                 return this;  
  444.             }  
  445.   
  446.             // required .Student.Sex sex = 3;  
  447.             public boolean hasSex() {  
  448.                 return result.hasSex();  
  449.             }  
  450.   
  451.             public com.carter.roster.proto.Roster.Student.Sex getSex() {  
  452.                 return result.getSex();  
  453.             }  
  454.   
  455.             public Builder setSex(com.carter.roster.proto.Roster.Student.Sex value) {  
  456.                 if (value == null) {  
  457.                     throw new NullPointerException();  
  458.                 }  
  459.                 result.hasSex = true;  
  460.                 result.sex_ = value;  
  461.                 return this;  
  462.             }  
  463.   
  464.             public Builder clearSex() {  
  465.                 result.hasSex = false;  
  466.                 result.sex_ = com.carter.roster.proto.Roster.Student.Sex.MALE;  
  467.                 return this;  
  468.             }  
  469.   
  470.             // @@protoc_insertion_point(builder_scope:Student)  
  471.         }  
  472.   
  473.         static {  
  474.             defaultInstance = new Student(true);  
  475.             com.carter.roster.proto.Roster.internalForceInit();  
  476.             defaultInstance.initFields();  
  477.         }  
  478.   
  479.         // @@protoc_insertion_point(class_scope:Student)  
  480.     }  
  481.   
  482.     public static final class StudentRoster extends com.google.protobuf.GeneratedMessage {  
  483.         // Use StudentRoster.newBuilder() to construct.  
  484.         private StudentRoster() {  
  485.             initFields();  
  486.         }  
  487.   
  488.         private StudentRoster(boolean noInit) {  
  489.         }  
  490.   
  491.         private static final StudentRoster defaultInstance;  
  492.   
  493.         public static StudentRoster getDefaultInstance() {  
  494.             return defaultInstance;  
  495.         }  
  496.   
  497.         public StudentRoster getDefaultInstanceForType() {  
  498.             return defaultInstance;  
  499.         }  
  500.   
  501.         public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() {  
  502.             return com.carter.roster.proto.Roster.internal_static_StudentRoster_descriptor;  
  503.         }  
  504.   
  505.         protected com.google.protobuf.GeneratedMessage.FieldAccessorTable internalGetFieldAccessorTable() {  
  506.             return com.carter.roster.proto.Roster.internal_static_StudentRoster_fieldAccessorTable;  
  507.         }  
  508.   
  509.         // repeated .Student student = 1;  
  510.         public static final int STUDENT_FIELD_NUMBER = 1;  
  511.         private java.util.List<com.carter.roster.proto.Roster.Student> student_ = java.util.Collections.emptyList();  
  512.   
  513.         public java.util.List<com.carter.roster.proto.Roster.Student> getStudentList() {  
  514.             return student_;  
  515.         }  
  516.   
  517.         public int getStudentCount() {  
  518.             return student_.size();  
  519.         }  
  520.   
  521.         public com.carter.roster.proto.Roster.Student getStudent(int index) {  
  522.             return student_.get(index);  
  523.         }  
  524.   
  525.         private void initFields() {  
  526.         }  
  527.   
  528.         public final boolean isInitialized() {  
  529.             for (com.carter.roster.proto.Roster.Student element : getStudentList()) {  
  530.                 if (!element.isInitialized())  
  531.                     return false;  
  532.             }  
  533.             return true;  
  534.         }  
  535.   
  536.         public void writeTo(com.google.protobuf.CodedOutputStream output) throws java.io.IOException {  
  537.             getSerializedSize();  
  538.             for (com.carter.roster.proto.Roster.Student element : getStudentList()) {  
  539.                 output.writeMessage(1, element);  
  540.             }  
  541.             getUnknownFields().writeTo(output);  
  542.         }  
  543.   
  544.         private int memoizedSerializedSize = -1;  
  545.   
  546.         public int getSerializedSize() {  
  547.             int size = memoizedSerializedSize;  
  548.             if (size != -1)  
  549.                 return size;  
  550.   
  551.             size = 0;  
  552.             for (com.carter.roster.proto.Roster.Student element : getStudentList()) {  
  553.                 size += com.google.protobuf.CodedOutputStream.computeMessageSize(1, element);  
  554.             }  
  555.             size += getUnknownFields().getSerializedSize();  
  556.             memoizedSerializedSize = size;  
  557.             return size;  
  558.         }  
  559.   
  560.         public static com.carter.roster.proto.Roster.StudentRoster parseFrom(com.google.protobuf.ByteString data)  
  561.                 throws com.google.protobuf.InvalidProtocolBufferException {  
  562.             return newBuilder().mergeFrom(data).buildParsed();  
  563.         }  
  564.   
  565.         public static com.carter.roster.proto.Roster.StudentRoster parseFrom(com.google.protobuf.ByteString data,  
  566.                 com.google.protobuf.ExtensionRegistryLite extensionRegistry)  
  567.                 throws com.google.protobuf.InvalidProtocolBufferException {  
  568.             return newBuilder().mergeFrom(data, extensionRegistry).buildParsed();  
  569.         }  
  570.   
  571.         public static com.carter.roster.proto.Roster.StudentRoster parseFrom(byte[] data)  
  572.                 throws com.google.protobuf.InvalidProtocolBufferException {  
  573.             return newBuilder().mergeFrom(data).buildParsed();  
  574.         }  
  575.   
  576.         public static com.carter.roster.proto.Roster.StudentRoster parseFrom(byte[] data,  
  577.                 com.google.protobuf.ExtensionRegistryLite extensionRegistry)  
  578.                 throws com.google.protobuf.InvalidProtocolBufferException {  
  579.             return newBuilder().mergeFrom(data, extensionRegistry).buildParsed();  
  580.         }  
  581.   
  582.         public static com.carter.roster.proto.Roster.StudentRoster parseFrom(java.io.InputStream input)  
  583.                 throws java.io.IOException {  
  584.             return newBuilder().mergeFrom(input).buildParsed();  
  585.         }  
  586.   
  587.         public static com.carter.roster.proto.Roster.StudentRoster parseFrom(java.io.InputStream input,  
  588.                 com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException {  
  589.             return newBuilder().mergeFrom(input, extensionRegistry).buildParsed();  
  590.         }  
  591.   
  592.         public static com.carter.roster.proto.Roster.StudentRoster parseDelimitedFrom(java.io.InputStream input)  
  593.                 throws java.io.IOException {  
  594.             Builder builder = newBuilder();  
  595.             if (builder.mergeDelimitedFrom(input)) {  
  596.                 return builder.buildParsed();  
  597.             } else {  
  598.                 return null;  
  599.             }  
  600.         }  
  601.   
  602.         public static com.carter.roster.proto.Roster.StudentRoster parseDelimitedFrom(java.io.InputStream input,  
  603.                 com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException {  
  604.             Builder builder = newBuilder();  
  605.             if (builder.mergeDelimitedFrom(input, extensionRegistry)) {  
  606.                 return builder.buildParsed();  
  607.             } else {  
  608.                 return null;  
  609.             }  
  610.         }  
  611.   
  612.         public static com.carter.roster.proto.Roster.StudentRoster  
  613.                 parseFrom(com.google.protobuf.CodedInputStream input) throws java.io.IOException {  
  614.             return newBuilder().mergeFrom(input).buildParsed();  
  615.         }  
  616.   
  617.         public static com.carter.roster.proto.Roster.StudentRoster  
  618.                 parseFrom(com.google.protobuf.CodedInputStream input,  
  619.                         com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException {  
  620.             return newBuilder().mergeFrom(input, extensionRegistry).buildParsed();  
  621.         }  
  622.   
  623.         public static Builder newBuilder() {  
  624.             return Builder.create();  
  625.         }  
  626.   
  627.         public Builder newBuilderForType() {  
  628.             return newBuilder();  
  629.         }  
  630.   
  631.         public static Builder newBuilder(com.carter.roster.proto.Roster.StudentRoster prototype) {  
  632.             return newBuilder().mergeFrom(prototype);  
  633.         }  
  634.   
  635.         public Builder toBuilder() {  
  636.             return newBuilder(this);  
  637.         }  
  638.   
  639.         public static final class Builder extends com.google.protobuf.GeneratedMessage.Builder<Builder> {  
  640.             private com.carter.roster.proto.Roster.StudentRoster result;  
  641.   
  642.             // Construct using com.carter.roster.proto.Roster.StudentRoster.newBuilder()  
  643.             private Builder() {  
  644.             }  
  645.   
  646.             private static Builder create() {  
  647.                 Builder builder = new Builder();  
  648.                 builder.result = new com.carter.roster.proto.Roster.StudentRoster();  
  649.                 return builder;  
  650.             }  
  651.   
  652.             protected com.carter.roster.proto.Roster.StudentRoster internalGetResult() {  
  653.                 return result;  
  654.             }  
  655.   
  656.             public Builder clear() {  
  657.                 if (result == null) {  
  658.                     throw new IllegalStateException("Cannot call clear() after build().");  
  659.                 }  
  660.                 result = new com.carter.roster.proto.Roster.StudentRoster();  
  661.                 return this;  
  662.             }  
  663.   
  664.             public Builder clone() {  
  665.                 return create().mergeFrom(result);  
  666.             }  
  667.   
  668.             public com.google.protobuf.Descriptors.Descriptor getDescriptorForType() {  
  669.                 return com.carter.roster.proto.Roster.StudentRoster.getDescriptor();  
  670.             }  
  671.   
  672.             public com.carter.roster.proto.Roster.StudentRoster getDefaultInstanceForType() {  
  673.                 return com.carter.roster.proto.Roster.StudentRoster.getDefaultInstance();  
  674.             }  
  675.   
  676.             public boolean isInitialized() {  
  677.                 return result.isInitialized();  
  678.             }  
  679.   
  680.             public com.carter.roster.proto.Roster.StudentRoster build() {  
  681.                 if (result != null && !isInitialized()) {  
  682.                     throw newUninitializedMessageException(result);  
  683.                 }  
  684.                 return buildPartial();  
  685.             }  
  686.   
  687.             private com.carter.roster.proto.Roster.StudentRoster buildParsed()  
  688.                     throws com.google.protobuf.InvalidProtocolBufferException {  
  689.                 if (!isInitialized()) {  
  690.                     throw newUninitializedMessageException(result).asInvalidProtocolBufferException();  
  691.                 }  
  692.                 return buildPartial();  
  693.             }  
  694.   
  695.             public com.carter.roster.proto.Roster.StudentRoster buildPartial() {  
  696.                 if (result == null) {  
  697.                     throw new IllegalStateException("build() has already been called on this Builder.");  
  698.                 }  
  699.                 if (result.student_ != java.util.Collections.EMPTY_LIST) {  
  700.                     result.student_ = java.util.Collections.unmodifiableList(result.student_);  
  701.                 }  
  702.                 com.carter.roster.proto.Roster.StudentRoster returnMe = result;  
  703.                 result = null;  
  704.                 return returnMe;  
  705.             }  
  706.   
  707.             public Builder mergeFrom(com.google.protobuf.Message other) {  
  708.                 if (other instanceof com.carter.roster.proto.Roster.StudentRoster) {  
  709.                     return mergeFrom((com.carter.roster.proto.Roster.StudentRoster) other);  
  710.                 } else {  
  711.                     super.mergeFrom(other);  
  712.                     return this;  
  713.                 }  
  714.             }  
  715.   
  716.             public Builder mergeFrom(com.carter.roster.proto.Roster.StudentRoster other) {  
  717.                 if (other == com.carter.roster.proto.Roster.StudentRoster.getDefaultInstance())  
  718.                     return this;  
  719.                 if (!other.student_.isEmpty()) {  
  720.                     if (result.student_.isEmpty()) {  
  721.                         result.student_ = new java.util.ArrayList<com.carter.roster.proto.Roster.Student>();  
  722.                     }  
  723.                     result.student_.addAll(other.student_);  
  724.                 }  
  725.                 this.mergeUnknownFields(other.getUnknownFields());  
  726.                 return this;  
  727.             }  
  728.   
  729.             public Builder mergeFrom(com.google.protobuf.CodedInputStream input,  
  730.                     com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException {  
  731.                 com.google.protobuf.UnknownFieldSet.Builder unknownFields = com.google.protobuf.UnknownFieldSet  
  732.                         .newBuilder(this.getUnknownFields());  
  733.                 while (true) {  
  734.                     int tag = input.readTag();  
  735.                     switch (tag) {  
  736.                         case 0:  
  737.                             this.setUnknownFields(unknownFields.build());  
  738.                             return this;  
  739.                         default: {  
  740.                             if (!parseUnknownField(input, unknownFields, extensionRegistry, tag)) {  
  741.                                 this.setUnknownFields(unknownFields.build());  
  742.                                 return this;  
  743.                             }  
  744.                             break;  
  745.                         }  
  746.                         case 10: {  
  747.                             com.carter.roster.proto.Roster.Student.Builder subBuilder = com.carter.roster.proto.Roster.Student  
  748.                                     .newBuilder();  
  749.                             input.readMessage(subBuilder, extensionRegistry);  
  750.                             addStudent(subBuilder.buildPartial());  
  751.                             break;  
  752.                         }  
  753.                     }  
  754.                 }  
  755.             }  
  756.   
  757.             // repeated .Student student = 1;  
  758.             public java.util.List<com.carter.roster.proto.Roster.Student> getStudentList() {  
  759.                 return java.util.Collections.unmodifiableList(result.student_);  
  760.             }  
  761.   
  762.             public int getStudentCount() {  
  763.                 return result.getStudentCount();  
  764.             }  
  765.   
  766.             public com.carter.roster.proto.Roster.Student getStudent(int index) {  
  767.                 return result.getStudent(index);  
  768.             }  
  769.   
  770.             public Builder setStudent(int index, com.carter.roster.proto.Roster.Student value) {  
  771.                 if (value == null) {  
  772.                     throw new NullPointerException();  
  773.                 }  
  774.                 result.student_.set(index, value);  
  775.                 return this;  
  776.             }  
  777.   
  778.             public Builder setStudent(int index, com.carter.roster.proto.Roster.Student.Builder builderForValue) {  
  779.                 result.student_.set(index, builderForValue.build());  
  780.                 return this;  
  781.             }  
  782.   
  783.             public Builder addStudent(com.carter.roster.proto.Roster.Student value) {  
  784.                 if (value == null) {  
  785.                     throw new NullPointerException();  
  786.                 }  
  787.                 if (result.student_.isEmpty()) {  
  788.                     result.student_ = new java.util.ArrayList<com.carter.roster.proto.Roster.Student>();  
  789.                 }  
  790.                 result.student_.add(value);  
  791.                 return this;  
  792.             }  
  793.   
  794.             public Builder addStudent(com.carter.roster.proto.Roster.Student.Builder builderForValue) {  
  795.                 if (result.student_.isEmpty()) {  
  796.                     result.student_ = new java.util.ArrayList<com.carter.roster.proto.Roster.Student>();  
  797.                 }  
  798.                 result.student_.add(builderForValue.build());  
  799.                 return this;  
  800.             }  
  801.   
  802.             public Builder addAllStudent(java.lang.Iterable<? extends com.carter.roster.proto.Roster.Student> values) {  
  803.                 if (result.student_.isEmpty()) {  
  804.                     result.student_ = new java.util.ArrayList<com.carter.roster.proto.Roster.Student>();  
  805.                 }  
  806.                 super.addAll(values, result.student_);  
  807.                 return this;  
  808.             }  
  809.   
  810.             public Builder clearStudent() {  
  811.                 result.student_ = java.util.Collections.emptyList();  
  812.                 return this;  
  813.             }  
  814.   
  815.             // @@protoc_insertion_point(builder_scope:StudentRoster)  
  816.         }  
  817.   
  818.         static {  
  819.             defaultInstance = new StudentRoster(true);  
  820.             com.carter.roster.proto.Roster.internalForceInit();  
  821.             defaultInstance.initFields();  
  822.         }  
  823.   
  824.         // @@protoc_insertion_point(class_scope:StudentRoster)  
  825.     }  
  826.   
  827.     private static com.google.protobuf.Descriptors.Descriptor internal_static_Student_descriptor;  
  828.     private static com.google.protobuf.GeneratedMessage.FieldAccessorTable internal_static_Student_fieldAccessorTable;  
  829.     private static com.google.protobuf.Descriptors.Descriptor internal_static_StudentRoster_descriptor;  
  830.     private static com.google.protobuf.GeneratedMessage.FieldAccessorTable internal_static_StudentRoster_fieldAccessorTable;  
  831.   
  832.     public static com.google.protobuf.Descriptors.FileDescriptor getDescriptor() {  
  833.         return descriptor;  
  834.     }  
  835.   
  836.     private static com.google.protobuf.Descriptors.FileDescriptor descriptor;  
  837.     static {  
  838.         java.lang.String[] descriptorData = { "\n\014roster.proto\"[\n\007Student\022\n\n\002id\030\001 \002(\005\022\014\n"  
  839.                 + "\004name\030\002 \002(\t\022\031\n\003sex\030\003 \002(\0162\014.Student.Sex\"\033"  
  840.                 + "\n\003Sex\022\010\n\004MALE\020\000\022\n\n\006FEMALE\020\001\"*\n\rStudentRo"  
  841.                 + "ster\022\031\n\007student\030\001 \003(\0132\010.StudentB!\n\027com.c"  
  842.                 + "arter.roster.protoB\006Roster" };  
  843.         com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = new com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner() {  
  844.             public com.google.protobuf.ExtensionRegistry assignDescriptors(  
  845.                     com.google.protobuf.Descriptors.FileDescriptor root) {  
  846.                 descriptor = root;  
  847.                 internal_static_Student_descriptor = getDescriptor().getMessageTypes().get(0);  
  848.                 internal_static_Student_fieldAccessorTable = new com.google.protobuf.GeneratedMessage.FieldAccessorTable(  
  849.                         internal_static_Student_descriptor, new java.lang.String[] { "Id""Name""Sex", },  
  850.                         com.carter.roster.proto.Roster.Student.class,  
  851.                         com.carter.roster.proto.Roster.Student.Builder.class);  
  852.                 internal_static_StudentRoster_descriptor = getDescriptor().getMessageTypes().get(1);  
  853.                 internal_static_StudentRoster_fieldAccessorTable = new com.google.protobuf.GeneratedMessage.FieldAccessorTable(  
  854.                         internal_static_StudentRoster_descriptor, new java.lang.String[] { "Student", },  
  855.                         com.carter.roster.proto.Roster.StudentRoster.class,  
  856.                         com.carter.roster.proto.Roster.StudentRoster.Builder.class);  
  857.                 return null;  
  858.             }  
  859.         };  
  860.         com.google.protobuf.Descriptors.FileDescriptor.internalBuildGeneratedFileFrom(descriptorData,  
  861.                 new com.google.protobuf.Descriptors.FileDescriptor[] {}, assigner);  
  862.     }  
  863.   
  864.     public static void internalForceInit() {  
  865.     }  
  866.   
  867.     // @@protoc_insertion_point(outer_class_scope)  
  868. }  



步骤三、将介绍部分打包生成的jar包导入到该Android工程中,因为日后会用到。

导入成功后,我们就可以使用jar包中的API了。


**********************************************************************华丽丽的分割线******************************************************************************************************

**********************************************************************上面是配环境等,下面是开始Android程序开发******************************************************************************************************


步骤四、开发Android项目。先新建一个布局文件,有三个EditText,下面一个Button,最下面是一个ListView。顺带着写了ListView每一项的布局文件。

main.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <LinearLayout  
  8.         android:id="@+id/id_ll"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:orientation="horizontal"  
  12.         >  
  13.           
  14.         <TextView  
  15.             android:layout_width="wrap_content"  
  16.             android:layout_height="wrap_content"  
  17.             android:textSize="20dp"  
  18.             android:text="学号:"  
  19.             />  
  20.           
  21.         <EditText  
  22.             android:id="@+id/id_et"  
  23.             android:layout_width="fill_parent"   
  24.             android:layout_height="wrap_content"  
  25.             />  
  26.           
  27.     </LinearLayout>  
  28.       
  29.     <LinearLayout  
  30.         android:id="@+id/name_ll"  
  31.         android:layout_width="fill_parent"  
  32.         android:layout_height="wrap_content"  
  33.         android:orientation="horizontal"  
  34.         >  
  35.           
  36.         <TextView  
  37.             android:layout_width="wrap_content"  
  38.             android:layout_height="wrap_content"  
  39.             android:textSize="20dp"  
  40.             android:text="姓名:"  
  41.             />  
  42.           
  43.         <EditText  
  44.             android:id="@+id/name_et"  
  45.             android:layout_width="fill_parent"   
  46.             android:layout_height="wrap_content"  
  47.             />  
  48.           
  49.     </LinearLayout>  
  50.       
  51.     <LinearLayout  
  52.         android:id="@+id/sex_ll"  
  53.         android:layout_width="fill_parent"  
  54.         android:layout_height="wrap_content"  
  55.         android:orientation="horizontal"  
  56.         >  
  57.           
  58.         <TextView  
  59.             android:layout_width="wrap_content"  
  60.             android:layout_height="wrap_content"  
  61.             android:textSize="20dp"  
  62.             android:text="性别:"  
  63.             />  
  64.           
  65.         <EditText  
  66.             android:id="@+id/sex_et"  
  67.             android:layout_width="fill_parent"   
  68.             android:layout_height="wrap_content"  
  69.             />  
  70.           
  71.     </LinearLayout>  
  72.       
  73.     <Button   
  74.         android:id="@+id/submit_bt"  
  75.         android:layout_width="wrap_content"  
  76.         android:layout_height="wrap_content"  
  77.         android:text="提  交"  
  78.         android:textSize="20dp"  
  79.         android:layout_gravity="center"  
  80.         />  
  81.       
  82.     <ListView   
  83.         android:id="@+id/list_lv"  
  84.         android:layout_width="fill_parent"  
  85.         android:layout_height="fill_parent"  
  86.         />  
  87.   
  88.   
  89. </LinearLayout>  

adapter_item.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="horizontal" >  
  6.       
  7.     <TextView  
  8.         android:id="@+id/id_tv"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:gravity="center"  
  12.         android:layout_weight="1"  
  13.         android:text="1"  
  14.         />  
  15.     <TextView  
  16.         android:id="@+id/name_tv"  
  17.         android:layout_width="fill_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:gravity="center"  
  20.         android:layout_weight="1"  
  21.         android:text="1"  
  22.         />  
  23.     <TextView  
  24.         android:id="@+id/sex_tv"  
  25.         android:layout_width="fill_parent"  
  26.         android:layout_height="wrap_content"  
  27.         android:gravity="center"  
  28.         android:layout_weight="1"  
  29.         android:text="1"  
  30.         />  
  31.       
  32.   
  33. </LinearLayout>  


步骤五、定义一个StudentInfo类,封装学号、姓名、性别三个属性。

[java] view plaincopy
  1. package com.carter.roster;  
  2.   
  3. public class StudentInfo {  
  4.       
  5.     private int id;  
  6.     private String name;  
  7.     private String sex;  
  8.       
  9.       
  10.     public int getId() {  
  11.         return id;  
  12.     }  
  13.     public void setId(int id) {  
  14.         this.id = id;  
  15.     }  
  16.     public String getName() {  
  17.         return name;  
  18.     }  
  19.     public void setName(String name) {  
  20.         this.name = name;  
  21.     }  
  22.     public String getSex() {  
  23.         return sex;  
  24.     }  
  25.     public void setSex(String sex) {  
  26.         this.sex = sex;  
  27.     }  
  28.       
  29.       
  30.   
  31. }  


步骤六、定义一个helper类,用来辅助程序完成有关使用PB的功能,例如通过PB写入、读取数据。完成显示和逻辑的分离。

[java] view plaincopy
  1. package com.carter.roster;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.util.ArrayList;  
  8. import java.util.List;  
  9.   
  10. import com.carter.roster.proto.Roster.Student;  
  11. import com.carter.roster.proto.Roster.Student.Sex;  
  12. import com.carter.roster.proto.Roster.StudentRoster;  
  13.   
  14.   
  15. /** 
  16.  * A helper that dealing with Protocol Buffer. For example, save or read student info using Protocol Buffer.</p> 
  17.  * There're two function that will be convenient to use.</p> 
  18.  * getStudentsFromFile() & saveStudentInfoFile(int, String, String)</p> 
  19.  * The first one is used to save student info into a .dat file, and the other one is used to read from it. 
  20.  */  
  21. public class InfoHelper {  
  22.       
  23.     /** 
  24.      * File saved in SDCard. 
  25.      */  
  26.     public static final String FILE_NAME = "/mnt/sdcard/roster.dat";  
  27.       
  28.       
  29.     /** 
  30.      * Read student infos from the .dat file. 
  31.      * @return list the ArrayList encapsulated several StudentInfo objects.  
  32.      */  
  33.     public static List<StudentInfo> getStudentsFromFile(){  
  34.           
  35.         ArrayList<StudentInfo> list = new ArrayList<StudentInfo>();  
  36.         FileInputStream fis = null;  
  37.           
  38.         try {  
  39.             fis = new FileInputStream(FILE_NAME);  
  40.             StudentRoster roster = StudentRoster.parseFrom(fis);  
  41.             int student_count = roster.getStudentCount();  
  42.               
  43.             for(int i=0; i<student_count; i++){  
  44.                 Student student = roster.getStudent(i);  
  45.                 StudentInfo info = new StudentInfo();  
  46.                 info.setId(student.getId());  
  47.                 info.setName(student.getName());  
  48.                 info.setSex(student.getSex().toString());  
  49.                 list.add(info);  
  50.             }  
  51.               
  52.         } catch (FileNotFoundException e) {  
  53.             // TODO Auto-generated catch block  
  54.             e.printStackTrace();  
  55.         } catch (IOException e) {  
  56.             // TODO Auto-generated catch block  
  57.             e.printStackTrace();  
  58.         }finally{  
  59.             if(null!=fis){  
  60.                 try {  
  61.                     fis.close();  
  62.                 } catch (IOException e) {  
  63.                     // TODO Auto-generated catch block  
  64.                     e.printStackTrace();  
  65.                 }  
  66.             }  
  67.         }  
  68.           
  69.         return list;  
  70.     }  
  71.       
  72.       
  73.       
  74.     /** 
  75.      * Save a student info using Protocol Buffer by the given infomation. 
  76.      * @param id student's id 
  77.      * @param name student's name 
  78.      * @param sex student's sex-type.MALE/FEMALE 
  79.      */  
  80.     public static void saveStudentIntoFile(int id, String name, String sex){  
  81.         FileOutputStream fos = null;  
  82.           
  83.         StudentRoster.Builder roster = StudentRoster.newBuilder();  
  84.         Student.Builder student = Student.newBuilder();  
  85.         student.setId(id);  
  86.         student.setName(name);  
  87.         if("MALE".equalsIgnoreCase(sex)){  
  88.             student.setSex(Sex.MALE);  
  89.         }else{  
  90.             student.setSex(Sex.FEMALE);  
  91.         }  
  92.           
  93.         roster.addStudent(student.build());  
  94.           
  95.         try {  
  96.             fos = new FileOutputStream(FILE_NAME, true);  
  97.             roster.build().writeTo(fos);  
  98.             fos.close();  
  99.         } catch (FileNotFoundException e) {  
  100.             // TODO Auto-generated catch block  
  101.             e.printStackTrace();  
  102.         } catch (IOException e) {  
  103.             // TODO Auto-generated catch block  
  104.             e.printStackTrace();  
  105.         }  
  106.           
  107.           
  108.     }  
  109.   
  110. }  

里面定义了两个方法,public static List<StudentInfo> getStudentsFromFile() 和 public static void saveStudentIntoFile(int id, String name, String sex),分别用于读取和保存。

里面的方法体都很简单,由于只是实现一下Demo,就很多情况没有考虑,如果有朋友在使用的时候发现有ANR或者FC,请自行修复吧呵呵~~~


上面就是我写的代码了,两个方法是非常重要的。由于PB自带的那个例子感觉有些复杂,我就提炼了一下自己写了一个,有想看原来的可以去\examples\目录下查看。


好吧,先写到这里。

我会把项目源码都打包传上下载页,请去CSDN下载页面下载。小弟辛苦的整理的,给个辛苦分1分哈~~~要求不多~~~

项目源码下载


0 0