Created
December 7, 2019 20:34
-
-
Save Ahmedbadereldin/ca31d2aa0ba90da02a45f92a2fb617f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ApiInterface | |
@Multipart | |
@POST("user/add-image") | |
Call<ResultAPIModel<MemberModelX>> uploadProfileImage(@HeaderMap() Map<String, Object> headerParams, @Part MultipartBody.Part image); | |
public void uploadImage(String uri) { | |
File file = new File(uri); | |
RequestBody requestImage = RequestBody.create(MediaType.parse("multipart/form-data"), file); | |
MultipartBody.Part part = MultipartBody.Part.createFormData("image", file.getName(), requestImage); | |
Call<ResultAPIModel<MemberModelX>> call = apiService.uploadProfileImage(headerMap, part); | |
call.enqueue(new Callback<ResultAPIModel<MemberModelX>>() { | |
@Override | |
public void onResponse(Call<ResultAPIModel<MemberModelX>> call, Response<ResultAPIModel<MemberModelX>> response) { | |
ResultAPIModel<MemberModelX> result = response.body(); | |
if (response.isSuccessful()) { | |
dataFetcherCallBack.Result(result, "uploadImage", true); | |
} else { | |
ResultAPIModel<ErrorModel> errorModel = null; | |
try { | |
String error = response.errorBody().string(); | |
Log.e("Log", "Log error " + error); | |
if (error != null) { | |
errorModel = new Gson().fromJson(error, new TypeToken<ResultAPIModel<ErrorModel>>() { | |
}.getType()); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
if (response.code() == 401) { | |
UtilityApp.logOut(); | |
openLoginActivity(); | |
} else { | |
dataFetcherCallBack.Result(errorModel, Constants.ERROR, false); | |
} | |
} | |
} | |
@Override | |
public void onFailure(Call<ResultAPIModel<MemberModelX>> call, Throwable t) { | |
t.printStackTrace(); | |
if (showProgress) | |
GlobalData.progressDialog(activity, "من فضلك انتظر", false); | |
if (t instanceof UnknownHostException || t instanceof NoRouteToHostException) { | |
dataFetcherCallBack.Result(null, Constants.FAIL, false); | |
} else { | |
dataFetcherCallBack.Result(null, Constants.FAIL, false); | |
} | |
} | |
}); | |
} | |
// in your code add | |
// ----- uploadImage(uri, true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment