使用此代碼:
private final WebApi api = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build().create(WebApi.class);
public void createGoal(String goalTitle, String goalDueDate, String goalComment, String goalCategory, String goalReminderDate, String account, File image, String userId, final CreateGoalHandler handler) {
if (image != null) {
RequestBody requestBodies = RequestBody.create(MediaType.parse("multipart/form-data"), image);
part = MultipartBody.Part.createFormData("profileImage", image.getName(), requestBodies);
}
RequestBody goalTtle = RequestBody.create(MediaType.parse("multipart/form-data"), goalTitle);
RequestBody goalDueDte = RequestBody.create(MediaType.parse("multipart/form-data"), goalDueDate);
RequestBody goalCmnt = RequestBody.create(MediaType.parse("multipart/form-data"), goalComment);
RequestBody goalCtgry = RequestBody.create(MediaType.parse("multipart/form-data"), goalCategory);
RequestBody goalReminderDtes = RequestBody.create(MediaType.parse("multipart/form-data"), goalReminderDate);
RequestBody accnt = RequestBody.create(MediaType.parse("multipart/form-data"), account);
RequestBody userid = RequestBody.create(MediaType.parse("multipart/form-data"), userId);
Call<CreateGoalPrp> callback = api.createGoal(goalTtle, goalDueDte, goalCmnt, goalCtgry, goalReminderDtes, accnt, part, userid);
callback.enqueue(new Callback<CreateGoalPrp>() {
@Override
public void onResponse(Call<CreateGoalPrp> call, Response<CreateGoalPrp> response) {
handler.OnSuccess(response.body());
}
@Override
public void onFailure(Call<CreateGoalPrp> call, Throwable t) {
handler.OnSuccess(null);
}
});
}
//////////////////////////// //
public interface WebApi {
@Multipart
@POST("createGoal.php")
Call<CreateGoalPrp> createGoal(@Part("goalTitle") RequestBody goalTitle, @Part("goalDueDate") RequestBody goalDueDate, @Part("goalComment") RequestBody goalComment, @Part("goalCategory") RequestBody goalCategory, @Part("goalReminderDate") RequestBody goalReminderDate, @Part("account") RequestBody account, @Part MultipartBody.Part profileImage, @Part("userId") RequestBody userId);