Reputation: 1
Android beginner here. I am having an issue with Multipart POST request. I am calling my API using POSTMAN and it returns code :200 but when i am calling it from my Application, it returns 503. I found out that this can happen because POSTMAN sends it as multipart by default. I looked through a lot of answers here but i couldn't relate them to my code.
How do i convert my current request into a multipart request?
Here is my interface:
@Multipart
@POST
Call<JsonObject> Login(@Url String url, @Body JsonObject LoginData);
My Interface is as follows :
public Call<JsonObject> Logincall(String teller_ID,String password,String ...}
/*somewhere around here i must do MultipartBody.Part...cant figure out where and how */
RetrofitAPI retrofitAPIObj = RETROBUILDER.create(RetrofitAPI.class);
JsonObject LoginData=new JsonObject();
LoginData.addProperty("teller_ID",teller_ID);
LoginData.addProperty("password",password);
LoginData.addProperty("branch",branch);
LoginData.addProperty("terminal",terminal);
LoginData.addProperty("isSecure",isSecure);
return retrofitAPIObj.Login(RetrofitURL.LOGIN, LoginData);
}
Thanks in advance
Upvotes: 0
Views: 668
Reputation: 3469
you can create a multipart Body with additional properties by following.
public MultipartBody createMultiPartBody(){
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);
builder.addFormDataPart("teller_ID",teller_ID);
builder.addFormDataPart("password",password);
builder.addFormDataPart("branch",branch);
builder.addFormDataPart("terminal",terminal);
builder.addFormDataPart("isSecure",isSecure);
MultipartBody requestBody = builder.build();
return requestBody;
}
Now, by calling this method, you will be getting multipartBody which you can parse by following code.
public static void uploadCropImage(String url, RequestBody requestBody, Callback<BasicResponse> callback) {
UploadMultiPartData uploadMultipartData = retrofit.create(UploadMultiPartData.class);
Call<ResponseType> call = uploadCropImageApi.uploadCropImage(url, requestBody);
call.enqueue(callback);
}
this is the interface.
public interface UploadMultiPartData {
@POST(UPLOAD_URL)
Call<ResponseType> uploadMultiPartData(
@Url String url,
@Body RequestBody requestBody);
}
Upvotes: 0
Reputation: 358
You can Call Api Format Like This There is parameter type Like JsonObject in post Method
Call<UploadHeadPicResponseModel> uploadHeadPic(@Part MultipartBody.Part file, @Part("json") RequestBody json);
public void doUploadHeadPic(@NonNull String filePath) {
if (!MNetworkUtil.isNetworkAvailable()) {
MToastUtil.show("网络不能连接");
return;
}
File file = new File(filePath);
String json = new Gson().toJson(new UploadHeadPicRequestModel());
if (!file.exists()) {
MToastUtil.show("文件不存在");
return;
}
progressDialog.show();
avatarSimpleDraweeView.setEnabled(false);
MApiManager.getService().uploadHeadPic(
MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file)),
RequestBody.create(MediaType.parse("multipart/form-data"), json))
.enqueue(new OnRetrofitCallbackListener<UploadHeadPicResponseModel>(mActivity) {
@Override
public void onSuccess(UploadHeadPicResponseModel responseModel) {
progressDialog.dismiss();
avatarSimpleDraweeView.setEnabled(true);
if (responseModel != null) {
String serverAvatarUrl = responseModel.data.headPicPath;
if (!TextUtils.isEmpty(serverAvatarUrl)) {
UserModel userModel = MUserManager.getInstance().getUser();
if (userModel != null) {
userModel.setAvatarUrl(serverAvatarUrl);
MUserManager.getInstance().updateOrInsertUserInfo(userModel);
MToastUtil.show("上传头像成功");
}
}
}
}
@Override
public void onFailure(int status, String failureMsg) {
progressDialog.dismiss();
avatarSimpleDraweeView.setEnabled(true);
MToastUtil.show((TextUtils.isEmpty(failureMsg) ? "上传失败" : failureMsg) + " : " + status);
}
});
}
Upvotes: 2