this answer use for MultipartBody and uploading multiple files.
my server-side codes are mvc development.
first, you need ApiService class like this:
public interface ApiService {
@POST("Home/UploadVideos")
Call<ResponseBody> postMeme(@Body RequestBody files);
}
and you need Apiclient like this:
public class ApiClient {
public static final String API_BASE_URL = "http://192.168.43.243/Web/";
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
private static Retrofit.Builder builder = new Retrofit.Builder().baseUrl(API_BASE_URL).addConverterFactory(GsonConverterFactory.create());
public static ApiService createService(Class<ApiService> serviceClass)
{
Retrofit retrofit = builder.client(httpClient.build()).build();
return retrofit.create(serviceClass);
}
}
after that you need ReqestBody class like this:
public class CountingFileRequestBody extends RequestBody {
private static final String TAG = "CountingFileRequestBody";
private final ProgressListener listener;
private final String key;
private final MultipartBody multipartBody;
protected CountingSink mCountingSink;
public CountingFileRequestBody(MultipartBody multipartBody,
String key,
ProgressListener listener) {
this.multipartBody = multipartBody;
this.listener = listener;
this.key = key;
}
@Override
public long contentLength() throws IOException {
return multipartBody.contentLength();
}
@Override
public MediaType contentType() {
return multipartBody.contentType();
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
mCountingSink = new CountingSink(sink);
BufferedSink bufferedSink = Okio.buffer(mCountingSink);
multipartBody.writeTo(bufferedSink);
bufferedSink.flush();
}
public interface ProgressListener {
void transferred(String key, int num);
}
protected final class CountingSink extends ForwardingSink {
private long bytesWritten = 0;
public CountingSink(Sink delegate) {
super(delegate);
}
@Override
public void write(Buffer source, long byteCount) throws IOException {
bytesWritten += byteCount;
listener.transferred(key, (int) (100F * bytesWritten / contentLength()));
super.write(source, byteCount);
delegate().flush(); // I have added this line to manually flush the sink
}
}
}
and finally, you need this code:
ApiService service = ApiClient.createService(ApiService.class);
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);
builder.addFormDataPart("files",file1.getName(), RequestBody.create(MediaType.parse("video/*"), file1));
builder.addFormDataPart("files",file3.getName(), RequestBody.create(MediaType.parse("video/*"), file3));
MultipartBody requestBody = builder.build();
CountingFileRequestBody requestBody1 = new CountingFileRequestBody(requestBody, "files", new CountingFileRequestBody.ProgressListener() {
@Override
public void transferred(String key, int num) {
Log.d("FinishAdapter","Perecentae is :"+num);
//update progressbar here
dialog.updateProgress(num);
if (num == 100){
dialog.dismiss();
}
}
});
Call<ResponseBody> call = service.postMeme(requestBody1);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
// Toast.makeText(getBaseContext(),"All fine",Toast.LENGTH_SHORT).show();
Log.d("FinishAdapter","every thing is ok............!");
Log.d("FinishAdapter",response.toString());
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
//Toast.makeText(getBaseContext(),t.getMessage(),Toast.LENGTH_SHORT).show();
Log.d("FinishAdapter","every thing is failed............!");
}
});
hope it helps.