2016-09-16 54 views
2

我希望通過@PartMap或@Part(「avatars_attributes []」)等方式發送陣列圖像到服務器。但沒有結果。也許你可以幫助我?Retrofit 2發送圖像陣列到服務器

對於@PartMap我使用HashMap中的一些內容:

 for(int i = 0; i < images.size(); i++){ 
       Bitmap btm = images.get(i); 
       ByteArrayOutputStream out = new ByteArrayOutputStream(); 
       btm.compress(Bitmap.CompressFormat.JPEG, 50, out); 
       byte[] profilePictureByte = out.toByteArray(); 
       final RequestBody imageBody = RequestBody.create(MediaType.parse("image/*"), profilePictureByte); 
       map.put("avatars_attributes[]\"; filename=\"avatars_attributes" + i + ".png\"",imageBody); 

@Multipart 
@POST(ApiRetrofitUrls.TRANSPORTS_URL) 
Call<TransportEntity> addTransport(@PartMap() Map<String, RequestBody> map); 

但服務器接收只空數組avatars_attributes [];

在另一方式中,我使用RequestBody或RequestBody []的ArrayList中,

但結果是一些。

謝謝。

+0

可以共享服務器請求合同 –

+0

其崗位要求和參數 「avatars_attributes []」 的圖像陣列 –

回答

0

試試這個

@POST(ApiRetrofitUrls.TRANSPORTS_URL) 
Call<TransportEntity> addTransport(@Body RequestBody data); 

MultipartBody.Builder builder = new MultipartBody.Builder(); 
    builder.setType(MultipartBody.FORM); 
    builder.addFormDataPart("content", textContent); 

    for(String filePath : imagePathList){ 
     File file = new File(filePath); 
     builder.addFormDataPart("avatars_attributes", file.getName(), 
       RequestBody.create(MediaType.parse("image/*"), file)); 
    } 

    MultipartBody requestBody = builder.build(); 
    Call<SocialCreateFeedResponse> call = mSocialClient.createFeeds(requestBody);