For updating a file on the server using API.
public void uploadProfilePic(File file) {
ApiClient.getClient().create(RetrofitApi.class).uploadUserProfile(getbody("123"), createBody(file)).enqueue(new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, Response<Result> response) {
if(response.isSuccessful()) {
if (response.body().getSuccess()) {
Toast.makeText(activity, response.body().getMessage(), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(activity, response.body().getMessage(), Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(activity, response.raw().message(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<Result> call, Throwable t) {
Toast.makeText(activity, t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
Other Methods.
public MultipartBody.Part createBody(File file) {
RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("profile", file.getName(), requestBody);
return body;
}
public RequestBody getbody(String descriptionString) {
return RequestBody.create(
okhttp3.MultipartBody.FORM, descriptionString);
}
Interface method ......
@Multipart
@POST("changePic")
Call<Result> uploadUserProfile(@Part("id") RequestBody id,@Part MultipartBody.Part image);
That is all. If any help related to this post please comment.
Thank you, guys.
Enjoy coding.
No comments:
Post a Comment