Android和Protobuf

时间:2011-10-15 11:49:01

标签: java android sdk protocol-buffers

我使用android sdk 2.1和protobuf 2.4.1

这是我的.proto文件:

package com.example.proto;

option optimize_for = LITE_RUNTIME;

message DatabaseInsertRequest {
  optional string stringInsert = 1;
}

message DatabaseSelectRequest {
  optional string stringSelect = 1;
}

message DatabaseUpdateRequest {
  optional string stringUpdate = 1;
}

我把这个.proto编译成了class(使用protoc)

我尝试用socket发送此消息后出错,(忘了) 你能告诉我工作实例吗?

2 个答案:

答案 0 :(得分:3)

查看文档!

JavaTutorial

答案 1 :(得分:0)

private void writeData() {
    long startTime;
    long totalTime;
    DefaultHttpClient client = new DefaultHttpClient();
    startTime = System.currentTimeMillis();
    HttpPost httpPost = new HttpPost(Constants.mSereverUrl + Constants.PORT + Constants.ContactRequest);
    try {
        httpPost.setEntity(new ByteArrayEntity(toBytes));
        HttpResponse response = client.execute(httpPost);
        totalTime = System.currentTimeMillis() - startTime;
        if (mCurrentMode == PROTO_MODE) {
            Constants.setProtoPostTime(totalTime);
        } else if (mCurrentMode == JSON_MODE) {
            Constants.setJsonPostTime(totalTime);
        }
        System.out.println("Request data sent");
        System.out.println("Response Code :: " + response.getStatusLine().getStatusCode());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

以上是ProtoBuf与Android集成的示例代码中的代码段。如果您需要进一步的帮助,请告诉我。