开发者

Efficiently sending protocol buffer messages with http on an android platform

I'm trying to send messages generated by Google Protocol Buffer code via a simple HTTP scheme to a server. What I have currently have implemented is here (forgive the obvious incompletion...):

 HttpClient client = new DefaultHttpClient();
 String url = "http://192.168.1.69:8888/sdroidmarshal";
 HttpPost postRequest = new HttpPost(url);

 String proto = offers.build().toString();
 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
 nameValuePairs.add(new BasicNameValuePair("sdroidmsg", proto));

 postRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));

 try {
   ResponseHandler<String> responseHandler = new BasicResponseHandler();
   String responseBody = client.execute(postRequest, responseHandler);

 } catch (Throwable t) {

 }

I'm not that experienced with communications over the internet and no more so with HTTP - while I do u开发者_运维技巧nderstand the basics... So my question, before I blindly develop the rest of the application around this, is whether or not this is particularly efficient? I ideally would like to keep messages small and I assume toString() adds some unnecessary formatting.


Message.toString() prints out the textual (non-serialized) representation. You probably want to use ToByteString() or ToByteArray() to get the serialized version, which you can then pass to your URLencoder.
Of course, that will incur some overhead for URL encoding. If you can handle multipart/form-data style POSTs, then you can just attach the bytes directly as a file-part, with application/octet-stream MIME-type; that way you'll be sending the smallest possible message, and won't have to worry about URL encoding / decoding on either end.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜