Skip to content

Instantly share code, notes, and snippets.

@NsAveek
Created October 20, 2018 05:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save NsAveek/52821fc4610c4dbfa7675a1ed4fd6007 to your computer and use it in GitHub Desktop.
Save NsAveek/52821fc4610c4dbfa7675a1ed4fd6007 to your computer and use it in GitHub Desktop.
Upload file using Retrofit
private void uploadFile(File file){
RequestBody videoBody = RequestBody.create(MediaType.parse("video/"), file);
MultipartBody.Part vFile = MultipartBody.Part.createFormData("upload-file", file.getName(), videoBody);
UploadVideoService videoService = RetrofitClientInstance.Companion.getRetrofitInstance();
Call<ResponseBody> serverCom=videoService.uploadVideo(vFile);
serverCom.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
ResponseBody body = response.body();
try {
String data = body.string();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
String message = t.getMessage();
String test = message;
}
});
}
class RetrofitClientInstance {
companion object {
fun getRetrofitInstance () : UploadVideoService{
val retrofit = Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("https://your base url here")
.build()
return retrofit.create(UploadVideoService::class.java)
}
}
}
interface UploadVideoService {
@Headers("Content-Type: video/mp4")
@Multipart
@PUT("/your endpoint here")
fun uploadVideo(@Part videoFile : MultipartBody.Part): Call<ResponseBody>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment