Hi, all changed the original json transmission format to protobuf because of business needs. The server side uses php and the client side uses android and ios. During the debugging process, it is found that the client has been unable to parse the data transmitted by the server, and the android client has always reported an exception, as shown below:... Hi, all
To meet service requirements, change the original json transmission format to protobuf. The server side uses php and the client side uses android and ios.
During the debugging process, it is found that the client has been unable to parse the data transmitted by the server, and the android client has always reported an exception, as shown below:
Protobuf error:Protocol message tag had invalid wire type
Background information
Php side
- Php5.4
- Porobuf-php
- Protoc 2.4.1
- Development in mac osx 10.8
Android end
- Android version 4.4
- Official protobuf library 2.4.1
- Protoc 2.4.1
- Ubuntu
Both php and android use the same set of *. proto files to generate corresponding classes.
Php is serialized as follows:
// use binary codec $codec = new \DrSlump\Protobuf\Codec\Binary(); // prepare final output $response = new \ResponseMsg(); $response->setStatusCode(\ResponseMsg\ErrorCode::RET_OK); $response->setData($codec->encode($recommend_bras)); // Use custom codec $data = $codec->encode($response); echo $data;
Android deserialization:
byte[] bytes = ret.result.getBytes();//ByteString byteString = ByteString.copyFromUtf8(ret.result);PResponseMsg.ResponseMsg responseMsg = PResponseMsg.ResponseMsg.parseFrom(bytes);
Reply content:
Hi, all
To meet service requirements, change the original json transmission format to protobuf. The server side uses php and the client side uses android and ios.
During the debugging process, it is found that the client has been unable to parse the data transmitted by the server, and the android client has always reported an exception, as shown below:
Protobuf error:Protocol message tag had invalid wire type
Background information
Php side
- Php5.4
- Porobuf-php
- Protoc 2.4.1
- Development in mac osx 10.8
Android end
- Android version 4.4
- Official protobuf library 2.4.1
- Protoc 2.4.1
- Ubuntu
Both php and android use the same set of *. proto files to generate corresponding classes.
Php is serialized as follows:
// use binary codec $codec = new \DrSlump\Protobuf\Codec\Binary(); // prepare final output $response = new \ResponseMsg(); $response->setStatusCode(\ResponseMsg\ErrorCode::RET_OK); $response->setData($codec->encode($recommend_bras)); // Use custom codec $data = $codec->encode($response); echo $data;
Android deserialization:
byte[] bytes = ret.result.getBytes();//ByteString byteString = ByteString.copyFromUtf8(ret.result);PResponseMsg.ResponseMsg responseMsg = PResponseMsg.ResponseMsg.parseFrom(bytes);
Find the solution and use base64 encoding to transfer the transmitted content.
My colleague asked me to use the binary method to find the problem. So I made the following attempt:
1. First save the serialized content of the server to the memory, and then reverse solve the problem. Preliminary conjecture may be that the content has a problem during transmission. Because the client and the server are transmitted over http, the content encoding format may be incorrect.
2. Ask the client's colleagues to help save the received content to the file system, and then compare the content on the server to check whether there is any encoding problem. Finally, we found that the content length of the two is different, and it is confirmed that there is a problem during the transmission.
3. I checked some online statements. binary content cannot be transmitted directly over http. Therefore, it should be base64-encoded before being transmitted. Then, the client can obtain and reverse solve the problem, you can get the final data.
I haven't figured out why the protobuf-php library will not work after the content is serialized and then transmitted. I want to add it ......