Search code examples
androidhttp-postmultipartentity

MultipartEntity not creating good request


I needed to send some XML to a webservices and I was able to do it with a normal StringEntity because it was just text but now I need to attach an image to it as well. I tried doing it with a MultipartEntity but I couldn't get it working with just the XML.

// Working

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost doc = new HttpPost("http://mywebservices.com");

HttpEntity entity = new StringEntity(writer.toString());
httppost.setEntity(entity);

HttpResponse response = httpclient.execute(httppost);
HttpEntity responseEntity = response.getEntity();

// not working

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost doc = new HttpPost("http://mywebservices.com");

// no difference when removing the BROWSER_COMPATIBLE   
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
entity.addPart("xml", new StringBody(writer.toString()));
httppost.setEntity(entity);

HttpResponse response = httpclient.execute(httppost);
HttpEntity responseEntity = response.getEntity();

And is there a way I could see the MIME that is being send?


Solution

  • You simply forgot:

    httppost.setEntity(entity);
    

    By the way, it's probably good form to set the Content-Type of your parts, e.g.:

    entity.addPart("xml", new StringBody(writer.toString(),"application/xml",Charset.forName("UTF-8")));
    

    As far as seeing what's being sent, see http://hc.apache.org/httpcomponents-client-ga/logging.html (especially "wire logging") for the HttpClient logging features, and this question for how to get it working on Android.