如何在android改造中发布图像和数据

时间:2019-05-22 04:30:36

标签: android retrofit2

我无法使用改造发布数据和图像。你能帮我吗

@Multipart
@POST("click_and_post")
Call<ResponseBody> clicPost(
    @Part ("click_and_post[image]") RequestBody file,
    @Part ("click_and_post[category_id]") String  category_id,
    @Part ("click_and_post[brand_id]") String  brand_id,
    @Part ("click_and_post[location]") String  location);

POst man 我无法发送数据,您能帮我吗

1 个答案:

答案 0 :(得分:1)

这是您应如何实现此api

    int size = youImagePathList.size();

    MultipartBody.Part[] multipartImageList = new MultipartBody.Part[size];

    if(size > 0) {

        for (int i = 0; i < size; i++) {
            File file = new File(notificationItemList.get(i).getImageEncoded());
            RequestBody surveyBody = RequestBody.create(MediaType.parse("image/*"), file);
            multipartImageList[i] = MultipartBody.Part.createFormData(""click_and_post[image]"", file.getName(), surveyBody);
        }

    }



RequestBody category_id = RequestBody.create(MediaType.parse("multipart/form-data"), StringCategoryID);
RequestBody brand_id = RequestBody.create(MediaType.parse("multipart/form-data"), StringBrandId);
RequestBody location = RequestBody.create(MediaType.parse("multipart/form-data"), StringLocation);

    @Multipart
    @POST("click_and_post")
    Call<ResponseBody> clicPost(
                @Header("Authorization") String authorization,  // if there is headers
                @Part  MultipartBody.Part[] multipartImageList,
                @Part("click_and_post[category_id]") RequestBody category_id,
                @Part("click_and_post[brand_id]") RequestBody brand_id,
                @Part("click_and_post[location]") RequestBody location);