开发者

Android: Sending a byte[] array via Http POST

I am able to do a POST of a parameters string. I use the following code:

String parameters = "firstname=john&lastname=doe";
URL url = new URL("http://www.mywebsite.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestPro开发者_如何学运维perty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestMethod("POST");

OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(parameters);
out.flush();
out.close();
connection.disconnect();

However, I need to do a POST of binary data (which is in form of byte[]).

Not sure how to change the above code to implement it.

Could anyone please help me with this?


Take a look here Sending POST data in Android

But use ByteArrayEntity.

byte[] content = ...
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new ByteArrayEntity(content));           
HttpResponse response = httpClient.execute(httpPost);


You could base-64 encode your data first. Take a look at the aptly named Base64 class.


These links might be helpful:

  • Android httpclient file upload data corruption and timeout issues
  • http://getablogger.blogspot.com/2008/01/android-how-to-post-file-to-php-server.html
  • http://forum.springsource.org/showthread.php?108546-How-do-I-post-a-byte-array
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜