Parsing a protocol message from Go by Java -
i write server(go)-client(java) programe, , use protobuf communication. define proto file , share between server , client. in server side:
- compile shared proto file go protoc
- serialize object proto.marshal
- send client make request service
in client side:
- compile shared proto file java protoc
- get bytes transferred via http
- deserialize bytes object.
here following error:
"com.google.protobuf.invalidprotocolbufferexception: while parsing protocol message, input ended unexpectedly in middle of field. mean either input has been truncated or embedded message misreported own length."
i confirmed http work fine , value of bytes received in client side same bytes send server. have same problem on this?
here proto file
syntax = "proto3"; package tutorial; message person { string name = 1; int32 id = 2; string email = 3; enum phonetype { mobile = 0; home = 1; work = 2; } message phonenumber { string number = 1; phonetype type = 2; } repeated phonenumber phones = 4; } message addressbook { repeated person people = 1; }
in go server side :
func todoindex(w http.responsewriter, r *http.request) { w.header().set("content-type", "application/x-protobuf") w.writeheader(http.statusok) p := &person{ id: 1234, name: "john doe", email: "jdoe@example.com", phones: []*person_phonenumber{ {number: "555-4321", type: person_home}, }, } out, err := proto.marshal(p) if err != nil { panic(err) } w.write(out) }
in java client side:
public class mainjavaandgo { public static void main(string[] args){ try { long start = (new date()).gettime(); system.out.println("begin get"); connect(); system.out.println("end get"); long time = (new date()).gettime() - start; }catch(exception e){ e.printstacktrace(); } } public static void connect(){ defaultbhttpclientconnection connection = new defaultbhttpclientconnection(8 * 1024); httphost server = hostforstring("localhost:8080"); try { socket socket = new socket(server.gethostname(), server.getport()); connection.bind(socket); httpcorecontext writecontext = httpcorecontext.create(); writecontext.settargethost(server); basichttpentityenclosingrequest httprequest = new basichttpentityenclosingrequest("get", "/todos"); httpprocessor httpproc = makehttpprocessor(); httprequestexecutor httpexecutor = new httprequestexecutor(); httpexecutor.preprocess(httprequest, httpproc, writecontext); httpresponse response = httpexecutor.execute(httprequest, connection, writecontext); httpexecutor.postprocess(response, httpproc, writecontext); inputstream inputstream = response.getentity().getcontent(); byte[] data = ioutils.tobytearray(inputstream); addressbook.addressbook addressbook = addressbook.addressbook.parsefrom(data); int foo = 0; foo++; }catch(exception e){ e.printstacktrace(); try { connection.shutdown(); }catch (exception ioe){ ioe.printstacktrace(); } } } public static httphost hostforstring(string hoststr) { string[] host = hoststr.split(":", 2); httphost httphost = new httphost(host[0], integer.parseint(host[1])); return httphost; } public static httpprocessor makehttpprocessor() { return httpprocessorbuilder.create().add(new requestcontent()).add(new requesttargethost()) .add(new requestconncontrol()).add(new requestuseragent("test protobuf/1.1")) .add(new requestexpectcontinue(true)).build(); } }
i got invalidprotocolbufferexception
com.google.protobuf.invalidprotocolbufferexception: while parsing protocol message, input ended unexpectedly in middle of field. mean either input has been truncated or embedded message misreported own length. @ com.google.protobuf.invalidprotocolbufferexception.truncatedmessage(invalidprotocolbufferexception.java:82) @ com.google.protobuf.codedinputstream$arraydecoder.skiprawbytes(codedinputstream.java:1200) @ com.google.protobuf.codedinputstream$arraydecoder.skipfield(codedinputstream.java:578) @ com.auth0.protobuf.addressbook$person.<init>(addressbook.java:112) @ com.auth0.protobuf.addressbook$person.<init>(addressbook.java:77) @ com.auth0.protobuf.addressbook$person$1.parsepartialfrom(addressbook.java:1817) @ com.auth0.protobuf.addressbook$person$1.parsepartialfrom(addressbook.java:1812) @ com.google.protobuf.codedinputstream$arraydecoder.readmessage(codedinputstream.java:816) @ com.auth0.protobuf.addressbook$addressbook.<init>(addressbook.java:1914) @ com.auth0.protobuf.addressbook$addressbook.<init>(addressbook.java:1871) @ com.auth0.protobuf.addressbook$addressbook$1.parsepartialfrom(addressbook.java:2571) @ com.auth0.protobuf.addressbook$addressbook$1.parsepartialfrom(addressbook.java:2566) @ com.google.protobuf.abstractparser.parsepartialfrom(abstractparser.java:163) @ com.google.protobuf.abstractparser.parsefrom(abstractparser.java:197) @ com.google.protobuf.abstractparser.parsefrom(abstractparser.java:209) @ com.google.protobuf.abstractparser.parsefrom(abstractparser.java:214) @ com.google.protobuf.abstractparser.parsefrom(abstractparser.java:49) @ com.auth0.protobuf.addressbook$addressbook.parsefrom(addressbook.java:2065) @ com.auth0.mainjavaandgo.connect(mainjavaandgo.java:78) @ com.auth0.mainjavaandgo.main(mainjavaandgo.java:35)
thank found answer comment. serialize , deserialize structure different. edited addressbook.person person = addressbook.person.parsefrom(data); , work.
Comments
Post a Comment