The protobuf data format reports "Protocolmessagetaghadinvalidwiretype" Exception

Source: Internet
Author: User
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 ......

Related Article

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.