在对接的第三方文档中有明确指明:
1 通信协议为HTTP
2 序列化协议使用protobuf3,
3 HTTP header头Content-Type:application/x-protobuf。
于是按照文档的.proto文件执行,中间参考了一些资料:https://blog.csdn.net/u012129607/article/details/95758405 ,最后自己进行序列化和反序列化都能没有问题。想着应该是问题不大,但是在实际的请求中对方返回错误:“I/O error while reading input message; nested exception is com.google.protobuf.InvalidProtocolBufferException: While parsing a protocol message, the input ended unexpectedly in the middle of a field. This could mean either that the input has been truncated or that an embedded message misreported its own length.。 第一反应难道是我的使用方式不对?上代码:
//序列化
$request = new \Request();
$request->setOs($requestParam['os_type'] == 'android' ? 2 : 1);
if ($requestParam['os_type'] == 'ios') {
$index = \DeviceType::value('IDFA');
$deviceId = $requestParam['imei'];
} else {
if (!empty($requestParam['imei'])) {
$index = \DeviceType::value('IMEI');
$deviceId = $requestParam['imei'];
} elseif (!empty($requestParam['android_id'])) {
$index = \DeviceType::value('ANDROID_ID');
$deviceId = $requestParam['android_id'];
} elseif (!empty($requestParam['oaid'])) {
$index = \DeviceType::value('OAID');
$deviceId = $requestParam['oaid'];
} elseif (!empty($requestParam['mac'])) {
$index = \DeviceType::value('MAC');
$deviceId = $requestParam['mac'];
} else {
$index = \DeviceType::value('IMEI');
$deviceId = $requestParam['imei'];
}
}
$request->setDeviceId($deviceId);
$request->setDeviceType($index);
$pack = $request->serializeToString();
//反序列化
$request = new \Request();
$request->mergeFromString($pack);
var_dump($request->getOs(), $request->getDeviceType(), $request->getDeviceId());
$options = [
'headers' => [
'Content-Type' => 'application/x-protobuf',
],
'json' => $pack
];
$response = $this->request(self::$ad_api, [], 'POST', $options);
响应结果:
{"timestamp":"2021-07-12T06:09:08.739+0000","status":400,"error":"Bad Request","message":"I/O error while reading input message; nested exception is com.google.protobuf.InvalidProtocolBufferException: While parsing a protocol message, the input ended unexpectedly in the middle of a field. This could mean either that the input has been truncated or that an embedded message misreported its own length.","path":"/api/rta/silent/common"}
难道,我这么使用理解上有问题吗还是?