1

I'm having an issue with a Multipart-form-data POST request using the Jersey Client API on Android. I've been following various examples on the web and they are all fairly similar in regards to the implementation.

Client client = createClientInstance();
WebResource r = client.resource(BASEURL).path("DataUpload");
ClientResponse post;
try {
    FormDataMultiPart multiPart = new FormDataMultiPart();
    multiPart.field("account", account);
    multiPart.field("checksum", checksum);
    multiPart.bodyPart(new FileDataBodyPart("file", file, MediaType.APPLICATION_OCTET_STREAM_TYPE));
    post = r.type(MediaType.MULTIPART_FORM_DATA)
        .accept(MediaType.TEXT_PLAIN)
        .post(ClientResponse.class, multiPart);

} catch (ClientHandlerException e) {
    Log.e(TAG, e.getLocalizedMessage());
} finally {
    client.destroy();
}

When I execute this code on my device, I am presented with an exception:

javax.ws.rs.WebApplicationException: java.lang.IllegalArgumentException: No MessageBodyWriter for body part of type 'java.io.File' and media type 'application/octet-stream'

I thought Jersey was supposed to handle File objects without any extra configuration. Removing the bodypart line will allow Jersey to make the request but that eliminates the point of this.

I have these libraries on my buildpath (which were pulled in with Maven):

  • jersey-client-1.14
  • jersey-core-1.14
  • jersey-multipart-1.14
  • mimepull-1-6
KC89
  • 11
  • 1
  • 2
  • http://stackoverflow.com/questions/9710953/trasferring-files-and-data-with-jersey should help? – smk Oct 21 '12 at 23:17
  • It seems my problem was related to the improper loading of the Jersey classes by Android. I was using the workaround that was listed [here](http://stackoverflow.com/questions/9342506/jersey-client-on-android-nullpointerexception/10676918#10676918) but that solution was incomplete as it was not loading all the classes required for my application. When I added the [complete class that was originally listed](http://jersey.576304.n2.nabble.com/java-lang-NullPointerException-on-Android-tt4212447.html#a5459910), it was able to do the file upload. – KC89 Oct 22 '12 at 14:27

1 Answers1

0

I can suggest two things to try:

  1. remove the MIME type from the FileDataBodyPart construction to see if Jersey can find a mime type it is happy to default to :

    multiPart.bodyPart(new FileDataBodyPart("file", file, MediaType.APPLICATION_OCTET_STREAM_TYPE));

  2. tell your client config about the multipart body writer (presumably inside your createClientInstance() method) :

    com.sun.jersey.api.client.config.ClientConfig config = new com.sun.jersey.api.client.config.DefaultClientConfig();
    config.getClasses().add(MultiPartWriter.class);
    client = Client.create(config);
    

Hope that helps.

Paul Jowett
  • 6,513
  • 2
  • 24
  • 19