Retrofit 2.0-beta-2 is adding literal quotes to MultiPart values

后端 未结 7 1740
终归单人心
终归单人心 2020-12-11 00:18

Went to upgrade to Retrofit 2.0 and running into this weird problem.

I have a method to log a user in

public interface ApiInterface {

    @Multipart         


        
7条回答
  •  Happy的楠姐
    2020-12-11 00:40

    I've found another one solution except those. Worked with Retrofit 2.1.0. (Rx adapter is optional here)

    My retrofit interface looks like this:

    @POST("/children/add")
    Observable addChild(@Body RequestBody requestBody);
    

    And in ApiManager I use it like this:

    @Override
        public Observable addChild(String firstName, String lastName, Long birthDate, @Nullable File passportPicture) {
            MultipartBody.Builder builder = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("first_name", firstName)
                    .addFormDataPart("last_name", lastName)
                    .addFormDataPart("birth_date", birthDate + "");
    
            //some nullable optional parameter
            if (passportPicture != null) {
                builder.addFormDataPart("certificate", passportPicture.getName(), RequestBody.create(MediaType.parse("image/*"), passportPicture));
            }
            return api.addChild(builder.build());
        }
    

    It is similar to Solution1 from Loyea but I think that it's little a bit more elegant.

提交回复
热议问题