Could you please help me with the Multipart Retrofit file upload with backend java rest api(mysql db). I tried with the below code but it is not working..
Retrofit part
Activity.java
private void checkingst(File file){
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),file);
MultipartBody.Part body = MultipartBody.Part.createFormData("file",file.getName(),requestBody);
Call<String> chkst = FarmOwnerApi.getServices().uploadFile(body);
chkst.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if(!response.isSuccessful())
{
return;
}
Toast.makeText(getApplicationContext(),"Uploaded",Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Toast.makeText(getApplicationContext(),"Failed",Toast.LENGTH_SHORT).show();
}
});
}
FarmOwnerApi.java
public static FarmOwnerService farmOwnerServices = null;
private static final String Base_URL = "http://192.168.1.6:8080/myfarm/farmowner/";
public static FarmOwnerService getServices()
{
if(farmOwnerServices==null)
{
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Base_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
farmOwnerServices = retrofit.create(FarmOwnerService.class);
}
return farmOwnerServices;
}
public interface FarmOwnerService {
@Multipart
@POST("uploadFile")
Call<String> uploadFile(@Part MultipartBody.Part file);
Backend - Java Rest API
@RequestMapping(value="/uploadFile" )
public String uploadFile(@RequestParam("file") MultipartFile file) {