0

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

Sandeep
  • 1
  • 1
  • 1
    Welcome to StackOverflow! Please improve your question with [edit]. You did not specify any question, it's just a statement of your experience. – Hille Mar 04 '19 at 08:46
  • Please check [jimmy0251's answer](https://stackoverflow.com/a/38891018/6115442). You will have to adjust your code. – berrytchaks Mar 04 '19 at 09:10
  • did you check this >https://androidclarified.com/android-image-upload-example/ – Adil Mar 04 '19 at 09:38

2 Answers2

2

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);
                }
            });
}
Aravind V
  • 358
  • 2
  • 10
  • 1
    How do i incorporate this into my interface? can i indicate this while i make the JSON object? – Sandeep Mar 04 '19 at 09:00
  • RequestBody mRBInput = null; try { mRBInput = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString().getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } – Aravind V Mar 04 '19 at 09:09
0

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);
}
Jay Dangar
  • 3,271
  • 1
  • 16
  • 35