Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpPost and byte[] on Android?

 
Adam Kronicki
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am dealing with http post method on Android. I have successfully send simple string pairs using HttpClient and HttpPost class with the NameValuePair as entity. However now I want to send a simple array of bytes[]. Unfortunately the only entities I have found for the HttpPost class are the NameValuePair and FileEntity. However my byte[] is not from a file. Wirting it to a file and then sending by the FileEntity seems to be pointless and stupid. How can I send this array using default HttpClient and HttpPost class??
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HTTP works with character data, not binary data. The easiest may be to still use a NameValuePair, after converting the byte[] to a String via the android.util.Base64 class.
 
Adam Kronicki
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. Since my data will still be base64 encoded this seems a good solution. However do I need to use the NameValuePair? I do not need to arrange the date in pairs - I just need to send the whole string...

I came upon another problem - in addition to a byte[] I also need to send a short[]. Can you also help with that problem?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you need to give it a name so that you can retrieve it in the servlet via getParameter - where you pass in the name.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic