把文件复制到proto下
先4双击 再1 打开cmd 再
protoc.exe --java_out ./ (要编译的文件名)2
再3中找到对应的文件放到android中
记得要修改2的包名跟项目中的一致!!!!
当然了先装上这个proto玩意er!再就是项目要有jar包(这就不说了)
复杂操作:
windows :
1,两个文件:proto.exe, protobuf-Java-2.4.1.jar
2,建立一个工程TestPb,在下面建立一个proto文件件,用来存放【.proto】文件
3,将proto,exe放在工程下,
4,建立一个msg.proto文件:
option java_package = "com.protobuftest.protobuf";
option java_outer_classname = "PersonProbuf";
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber phone = 4;
message CountryInfo {
required string name = 1;
required string code = 2;
optional int32 number = 3;
}
}
message AddressBook {
repeated Person person = 1;
}
5,生成 java文件:在proto.exe目录下:protoc --java_out=./src ./proto/msg.proto
6,copy个测试示例了
新建一个文件TestPb.java
***********************************************************
package com.protobuftest.protobuf;
import java.util.List;
import com.google.protobuf.InvalidProtocolBufferException;
import com.protobuftest.protobuf.PersonProbuf;
import com.protobuftest.protobuf.PersonProbuf.Person;
import com.protobuftest.protobuf.PersonProbuf.Person.PhoneNumber;
public class TestPb {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PersonProbuf.Person.Builder builder = PersonProbuf.Person.newBuilder();
builder.setEmail("kkk@email.com");
builder.setId(1);
builder.setName("TestName");
builder.addPhone(PersonProbuf.Person.PhoneNumber.newBuilder().setNumber("131111111").setType(PersonProbuf.Person.PhoneType.MOBILE));
builder.addPhone(PersonProbuf.Person.PhoneNumber.newBuilder().setNumber("011111").setType(PersonProbuf.Person.PhoneType.HOME));
Person person = builder.build();
byte[] buf = person.toByteArray();
try {
Person person2 = PersonProbuf.Person.parseFrom(buf);
System.out.println(person2.getName() + ", " + person2.getEmail());
List<PhoneNumber> lstPhones = person2.getPhoneList();
for (PhoneNumber phoneNumber : lstPhones) {
System.out.println(phoneNumber.getNumber());
}
} catch (InvalidProtocolBufferException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(buf);
}
}
***********************************************
*******************************
生成java文件:PersonProbuf.java
*******************************
工程文件结构: