改造图片上传无法处理的实体错误

Posted

技术标签:

【中文标题】改造图片上传无法处理的实体错误【英文标题】:Retrofit image upload Unprocessable Entity error 【发布时间】:2020-04-16 13:57:26 【问题描述】:

我正在使用改造多部分 API 上传图像。我在邮递员中取得了成功,但在代码中得到了以下错误:

响应protocol=http/1.1, code=422, message=Unprocessable Entity, url=http://upload-snack.13.251.251.232.nip.io/upload

邮递员:

改造代码:

请求:

 @Multipart
    @POST("upload")
    Call<ResponseBody> uploadImage(@Part MultipartBody.Part image);

界面:

 public static Retrofit getRetrofitClient(Context context, String baseURL) 
        if (retrofit == null) 
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .build();
            retrofit = new Retrofit.Builder()
                    .baseUrl(baseURL)
                    .client(okHttpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        
        return retrofit;
    

活动类代码:

 private void uploadToServer(String filePath) 
        Retrofit retrofit = ServiceGenerator.getRetrofitClient(this, "http://upload-snack.13.251.251.232.nip.io/");
        Api uploadAPIs = retrofit.create(Api.class);
        //Create a file object using file path
        File file = new File(filePath);
        // Create a request body with file and image media type
        RequestBody fileReqBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        // Create MultipartBody.Part using file request-body,file name and part name
        MultipartBody.Part part = MultipartBody.Part.createFormData("image", file.getName(), fileReqBody);
        //Create request body with text description and text media type
       // RequestBody description = RequestBody.create(MediaType.parse("text/plain"), "image-type");
        //
        Call call = uploadAPIs.uploadImage(part);
        call.enqueue(new Callback() 
            @Override
            public void onResponse(Call call, Response response) 
                Log.e("response", response.toString());
            

            @Override
            public void onFailure(Call call, Throwable t) 
                Log.e("failure", "failure");
            
        );
    

我检查了下面的教程/答案,但没有任何效果:

https://android.jlelse.eu/working-with-retrofit-825d30348fe2 https://inducesmile.com/android/android-upload-image-to-server-using-retrofit-2/ image upload using multipart retrofit 2 Upload image into server using retrofit https://www.simplifiedcoding.net/retrofit-upload-file-tutorial/ https://www.journaldev.com/23738/android-multipart-image-upload-progress-retrofit-nodejs Image upload using retrofit How to Upload Image file in Retrofit 2 Retrofit 2 Multipart image upload with data POST Multipart Form Data using Retrofit 2.0 including image Retrofit Uploading multiple images to a single key

还有更多.....

但仍然无法正常工作。请帮忙

【问题讨论】:

我也遇到了同样的问题,请问您找到解决方法了吗?如果有任何解决方案,请告诉我。 @Akshay 需要将媒体类型更改为 image/png 而不是 multipart/form-data。那会奏效的。 @VishvaDave 非常感谢,它现在正在工作。 【参考方案1】:

更改媒体类型 image/* multipart/form-data 的实例。我想这会对你有所帮助。

 File file = new File(filePath);
    // Create a request body with file and image media type
    RequestBody fileReqBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
    // Create MultipartBody.Part using file request-body,file name and part name
    MultipartBody.Part part = MultipartBody.Part.createFormData("image", file.getName(), fileReqBody);

【讨论】:

你确定键名是“image”【参考方案2】:

将您的 Retrofit 接口方法更改为此。

public class ServiceGenerator

    private static final String BASE_URL = "base_url";


    public static Retrofit getRetrofitClient() 
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(client)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
        return retrofit;
    

【讨论】:

【参考方案3】:

我在 Kotlin 中遇到了同样的问题,但感谢 @VishvaDave。于是我就这样做了

val file = File(filePath)
val fileReqBody = RequestBody.create(MediaType.parse("image/png"), file)
val part = MultipartBody.Part.createFormData("image", file.name, fileReqBody)

【讨论】:

以上是关于改造图片上传无法处理的实体错误的主要内容,如果未能解决你的问题,请参考以下文章

改造:在 POST @body 中上传图片

无法在 codeigniter 中上传图片,路径错误

CKEditor图片上传问题(默认安装情况下编辑器无法处理图片),通过Base64编码字符串解决

xampp中的Wordpress无法上传图片

改造vue-quill-editor: 结合element-ui上传图片到服务器

上传图片时显示上传文件与规定类型不符怎么办?