小编MaK*_*aKi的帖子

无法使用 Retrofit 2 上传图像

我无法从 Retrofit 2 上传图像,请查看我的下面的代码

@Multipart
@POST("upload-image")
Call<UploadImageResponse> uploadFile(@Part MultipartBody.Part file, @Part("file") RequestBody name);
Run Code Online (Sandbox Code Playgroud)

我调用的函数的执行方式是这样的,

// Uploading Image
public void uploadFile()
{
    // Map is used to multipart the file using okhttp3.RequestBody
    File file = new File(sellerActivity.mediaPath);

    // Parsing any Media type file
    RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);

    MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", file.getName(), requestBody);

    RequestBody filename = RequestBody.create(MediaType.parse("text/plain"), file.getName());

    Call<UploadImageResponse> call = apiService.uploadFile(fileToUpload, filename);

    call.enqueue(new Callback<UploadImageResponse>() {
        @Override
        public void onResponse(Call<UploadImageResponse> call, Response<UploadImageResponse> response) {
            if (response.isSuccessful())
            {
                System.out.println("Image Url: " …
Run Code Online (Sandbox Code Playgroud)

android image-uploading laravel retrofit2

5
推荐指数
1
解决办法
4620
查看次数

标签 统计

android ×1

image-uploading ×1

laravel ×1

retrofit2 ×1