我正在尝试在 Android 中使用 MultiPartEntity 和 HttpClient 发送图像,但不断收到异常:java.lang.UnsupportedOperationException: Multipart form entity does not implement #getContent()
这是我的代码:
public boolean enrollImage(String id, byte[] image) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("https://example.com/"+id+"/images/");
httpPost.addHeader("Authorization", "Basic " + Base64.encodeToString(("user"+":"+"password").getBytes(),Base64.NO_WRAP));
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("image", new ByteArrayBody(image, "image/jpg", "image.jpg"));
httpPost.setEntity(entity);
HttpResponse response;
try {
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
String responseString = EntityUtils.toString(entity);
JSONObject json = new JSONObject(responseString);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
我已将较新的 Apache 库httpmime-4.2.5.jar
、httpclient-4.2.5.jar
、httpcore-4.2.4.jar
和添加apache-mime4j-core-0.7.2.jar
到我的项目中。
我怎样才能让它运行,以便我可以发布