2016-09-18 20 views
0

我希望使用改進技術同時發送此Postdata和圖像文件。如何使用改進功能同時發送圖像和文本

POSTDATA

public class PostData implements Serializable { 
    @Expose 
    private String text; 
    @Expose 
    private Point point; 
} 
public class Point implements Serializable { 
    @Expose 
    private double longitude; 
    @Expose 
    private double latitude; 
} 

PostApiService

public interface PostApiService { 

    @Multipart 
    @POST("posts/") 
    Call<ResponseBody> uploadFile (@Part MultipartBody.Part part, @Body PostData postData); 
} 

我得到的圖像URI出這些代碼,我將使用它。它用作returnUri。 你可以考慮這個。

CODE:

view.findViewById(R.id.btn_post).setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void PostImageAndData(View view) { 

     Bitmap bitmap = null; 
     try { 
      bitmap = getBitmapFromUri(returnUri); #this method is to make Bitmap from Uri 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     File imageFile = null; 
     try { 
      imageFile = createFileFromBitmap(bitmap); #this method is to make File from Bitmap 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     OkHttpClient client = new OkHttpClient(); 
     OkHttpClient.Builder builder = new OkHttpClient.Builder(); 
     client = builder.build(); 
     Retrofit retrofit = new Retrofit.Builder() 
       .client(client) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .baseUrl(Constants.HTTP.BASE_URL) 
       .build(); 

     PostApiService postApiService = retrofit.create(PostApiService.class); 
     RequestBody requestFile = 
       RequestBody.create(MediaType.parse("multipart/form-data"), imageFile); 
     MultipartBody.Part body = 
       MultipartBody.Part.createFormData("image", makeImageFileName(), requestFile); #this method(makeimageFileName()) is for custom filename 
     Point mpoint = new Point(13, 15); 
     PostData postData = new PostData("hello", mpoint); 

     Call<ResponseBody> call = postApiService.uploadFile(body, postData); 
     call.enqueue(new Callback<ResponseBody>() { 
      @Override 
      public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 
       Toast.makeText(getContext(), "success?", Toast.LENGTH_LONG).show(); 
      } 
      @Override 
      public void onFailure(Call<ResponseBody> call, Throwable t) { 
      } 
     }); 
    } 
}); 

如果我在PostApiService使用@Body PostData postData,錯誤是java.lang.IllegalArgumentException: @Body parameters cannot be used with form or multi-part encoding. (parameter #2)

,如果我在PostApiService使用@Part PostData postData,錯誤是java.lang.IllegalArgumentException: @Part annotation must supply a name or use MultipartBody.Part parameter type. (parameter #2)

所以,我應該怎麼辦?

請幫助我嗎?

回答

1

昨天我有同樣的問題,我已經解決了。

它直接說兩種不同的格式是不允許的。 java.lang.IllegalArgumentException:@Body參數不能與表單或多部分編碼一起使用。 (參數#2)

您只需發送部分的所有數據。

@Multipart 
@POST("/api/Accounts/editaccount") 
Call<User> editUser (@Part("Authorization") String authorization,@Part("file\"; filename=\"pp.png\" ") RequestBody file , @Part("FirstName") RequestBody fname, @Part("Id") RequestBody id); 

就像這樣。

謝謝希望這對你有所幫助。

+0

對我而言,retrofit2比我預測的要難得多。謝謝。 Saveen –

+0

是的,我也是昨天:)別擔心 – Saveen

相關問題