如何使用改造同时发送图像和文本 [英] How to send image and text at the same time using retrofit

查看:92
本文介绍了如何使用改造同时发送图像和文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用改造同时发送此 Postdata 和图像文件.

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

PostDataPoint

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.你可以考虑这个.

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

代码:

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 参数不能与表单或多部分编码一起使用.(参数#2)

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)

如果我在 PostApiService 上使用 @Part PostData postData,错误是 java.lang.IllegalArgumentException:@Part 注释必须提供一个名称或使用 MultipartBody.Part 参数类型.(参数#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)

那我该怎么办?

请帮帮我好吗?

推荐答案

昨天我遇到了同样的问题,我已经解决了.

Yesterday i had same problem and i have solved it.

直接说两种不同的格式是不允许的.java.lang.IllegalArgumentException:@Body 参数不能与表单或多部分编码一起使用.(参数#2)

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

类似的东西.

谢谢,希望对您有所帮助.

Thanks hope this help you.

这篇关于如何使用改造同时发送图像和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆