使用HTTP Post发送字节数组而不使用MultipartEntity

时间:2014-06-20 10:44:07

标签: android

我需要将字节数组发布到web服务,但它不支持Multipart表单。所以,我搜索了一些其他的方法。我找到了这段代码:

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(address);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("key1", "value1"));
pairs.add(new BasicNameValuePair("key2", "value2"));
post.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = client.execute(post);

但它只允许我发送字符串,我想发送字节数组。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

将其作为字符串发布?

byte[] array = new byte[n];
array[0] = ..
array[n] = ..

String dataToSend = "";
For(int i = 0; i < array.length -1; i++){
dataToSend += (char) array[0];
}

// your post code here
...
pairs.add(new BasicNameValuePair("key1", dataToSend));