Question

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?

Était-ce utile?

La solution

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.

Autres conseils

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);
}

-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);

}

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top