如何使用多部分改造发送包含图像的自定义类对象数组

时间:2019-11-13 05:36:26

标签: android retrofit2 multipart

在我的android应用程序的本地database(Room)之一中,我有一个带有列(id(int),lat(double),lon(double)和image(blob))的表
我想使用改造将此表数据的数组推送到服务器。众所周知,要使用改造将图像上传到服务器,必须使用@Multipart

发送json array是一种选择,但为此,我必须将每个图像转换为Base64 string,并用Base64字符串上传图像是时候了消耗我不想使用它。

是否可以使用array分段将retrofit个对象发送到服务器

我可以使用此方法发送一条记录

@Multipart
@Post("")
Call<ApiResponse>syncDataToserver(@Part MultipartBody.Part image,@Part("lat")RequestBody lat,@Part("lon")RequestBody lon,@Part("id")RequestBody id); <br>

我们可以在下面发送它吗

@Multipart
    @POST("SyncPole")
    Call<String>uploadSurveyData(@PartMap HashMap<String, HashMap<String, Object>> map); <br>

我会这样发出请求

 HashMap<String,HashMap<String,Object>>map= new HashMap<>();
    if (list != null) {
        for (int i = 0; i < list.size(); i++) {

            HashMap<String,Object>hashMap = new HashMap<>();

            File file = new File(list.get(i).getImagePath());

            RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
            MultipartBody.Part fileupload = MultipartBody.Part.createFormData("file", file.getName(), requestBody);

            RequestBody polecode = RequestBody.create(MediaType.parse("text/plain"), list.get(i).getPoleCode());
            RequestBody latitude = RequestBody.create(MediaType.parse("text/plain"), String.valueOf(list.get(i).getLatitude()));

            RequestBody longitude = RequestBody.create(MediaType.parse("text/plain"), String.valueOf(list.get(i).getLongitude()));



            hashMap.put("image",fileupload);
            hashMap.put("pole_code",polecode);
            hashMap.put("latitude",latitude);
            hashMap.put("longitude",longitude);
            map.put(String.valueOf(i),hashMap);

        }
        Log.v("request",map.toString());
    }

0 个答案:

没有答案