How to send image and text at the same time using retrofit

耗尽温柔 提交于 2019-12-10 22:19:02

问题


I want send this Postdata and imagefile at the same time using retrofit.

PostData and Point

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);
}

I get image uri out of these codes, and i will use it. It used as returnUri. You may consider this.

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) {
            }
        });
    }
});

If i use @Body PostData postData on PostApiService, Error is java.lang.IllegalArgumentException: @Body parameters cannot be used with form or multi-part encoding. (parameter #2)

and If i use @Part PostData postData on PostApiService, Error is java.lang.IllegalArgumentException: @Part annotation must supply a name or use MultipartBody.Part parameter type. (parameter #2)

So, What should i do?

Please, Would you help me?


回答1:


Yesterday i had same problem and i have solved it.

It directly said two different format is not allowed. java.lang.IllegalArgumentException: @Body parameters cannot be used with form or multi-part encoding. (parameter #2)

You need to send all data in form of Part only.

@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);

Something like this.

Thanks hope this help you.



来源:https://stackoverflow.com/questions/39558595/how-to-send-image-and-text-at-the-same-time-using-retrofit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!