Reputation: 69
If, I'm using Postmam file upload without problems. But, when anroid tries upload file in server, method throws NPE, becouse MultipartFile file = null.
Android
RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
Call<MediaResponseModel> call = service.uploadImage(requestBody);
call.enqueue(new RetrofitCallback<>(callback));
@POST("/uploadImage")
Call<MediaResponseModel> uploadImage(@Part("file") RequestBody file);
Server
@RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<?> imageUpload(@RequestHeader(value = "id") String sessionId,
@RequestParam (value = "file") MultipartFile file)
ApplicationContext
<bean id="jsonConverter"
class="org.springframework.http.converter.json.GsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<mvc:annotation-driven >
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.GsonHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
Upvotes: 3
Views: 2803
Reputation:
It's difficult to do it on android using HTTP, there are some third party libraries you can use that.
Both are good libraries, I have used both in my projects.
Upvotes: 1
Reputation: 27
You can use Retrofit network library , it is the fastest one available
It supports multipart files and its implementation is very simple
http://square.github.io/retrofit/
Upvotes: 1