Reputation: 2853
I am trying to send an image using MultiPartEntity and HttpClient in Android, but keep getting the exception: java.lang.UnsupportedOperationException: Multipart form entity does not implement #getContent()
Here is my code:
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;
}
I've added the newer Apache libraries httpmime-4.2.5.jar
, httpclient-4.2.5.jar
, httpcore-4.2.4.jar
, and apache-mime4j-core-0.7.2.jar
to my project.
How can I get this this running so that I can POST
Upvotes: 2
Views: 3384
Reputation: 2853
It turns out that I was just confusing the request entity that I was sending with the response entity that I wanted to parse as json. Line 12 of that code should be
String responseString = EntityUtils.toString(responseEntity);
NOT
String responseString = EntityUtils.toString(entity);
Problem fixed. Guess it's time for me to buy a rubber duck
Upvotes: 2