2

I use android annotations to communicate with the server. In one of the api calls I need to send some text data and an image, say, from gallery.

@Post("/items/addItem.php")
String addItem(Protocol protocol);

How do I attach a MultipartForm with an image along with the post request?

Roman
  • 2,079
  • 4
  • 35
  • 53

3 Answers3

1

Just use the right Spring converter : FormHttpMessageConverter.

However, this converter only accepts MultiValueMap as method parameter. Please have a look at these two issues: #652 and #660.

If you really want to use any object as parameter, you have to implement your own custom FormHttpMessageConverter which will handle that by using reflection.

mkj
  • 2,761
  • 5
  • 24
  • 28
DayS
  • 1,561
  • 11
  • 15
1

DayS is right. An as quotation, you must include the FormHttpMessageConverter in your Rest Interface definition inside the converters array:

@Rest(rootUrl = "http://api.yourapp.com", converters = {
                MappingJacksonHttpMessageConverter.class,
                StringHttpMessageConverter.class, FormHttpMessageConverter.class })
public interface YourAppApiClient {

    @Post("/items/addItem.php")
    void getCustomerInformation(MultiValueMap formfields);
}
0

-Totaly Agree with above answers but for use of mappinjacksonhttpmessageconverter you have to add another library so if you dnt want to use it you can use below example

  • Or you can consider it as another example also :)

@Rest(rootUrl = CommonUtils.BASE_URL, converters = { ByteArrayHttpMessageConverter.class, FormHttpMessageConverter.class, StringHttpMessageConverter.class })

    public interface CustomRest extends RestClientErrorHandling{

    @Post(CommonUtils.pUrlLogin)
    String _Login(MultiValueMap<String, Object> multiValueMap);

    @Post(CommonUtils.pUrlSignUp)
    String _SignUp(MultiValueMap<String, Object> multiValueMap);

}

Hardy
  • 2,576
  • 1
  • 23
  • 45