上傳文件沒問題。上傳圖文,也就是上傳文字和圖片的時候,用以下這個代碼:
@Multipart
@POST("xxxxx/images/")
Flowable<UpImgAndTextBean> upLoadImgAndText(
@Part("title") String title,
@Part("content") String content,
@Part List<MultipartBody.Part> images);
你會發現,這兩個字符串的文本內容會自動帶上了雙引號……
廢話不說,解決方法就是把字符串的參數封裝成map,如下:
@Multipart
@POST("xxxxx/images/")
Flowable<UpImgAndTextBean> upLoadImgAndText(
@PartMap Map<String, RequestBody> params,
@Part List<MultipartBody.Part> images);
然後調用方法的時候,把這兩個字符串參數封裝一下,最後傳這個params給它即可:
Map<String, RequestBody> params = new HashMap<>();
params.put("title",RequestBody.create(MediaType.parse("text/plain"), title));
params.put("content",RequestBody.create(MediaType.parse("text/plain"), content));
注:如果要傳int類型的話需+“”轉成String類型。