【问题标题】:Unable to send image to the server无法将图像发送到服务器
【发布时间】:2019-06-07 08:01:23
【问题描述】:

这里是选择图库图片的代码

Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 2 && data != null && data.getData() != null) {
Uri uri = data.getData();
uploadFile(uri);
}
}
}

The function below is used to send an image to the server using Retrofit. For uploading the images the MultipartBody is used for sending the image as a file and used RequestBody for sending String request as Params but getting the error in sending an image file from the gallery.

    private void uploadFile(Uri fileUri) {
            File file = new File(fileUri.getPath());
            long sTime = System.currentTimeMillis();
            MultipartBody.Part fbody = MultipartBody.Part.create(RequestBody.create(MediaType.parse("image/*"), file));

    RequestBody name = RequestBody.create(MediaType.parse("text/plain"),sTime + "FramePlayer.Jpeg");
    RequestBody id = RequestBody.create(MediaType.parse("text/plain"), "1498");
    RequestBody type = RequestBody.create(MediaType.parse("text/plain"), "player");
    RequestBody gif = RequestBody.create(MediaType.parse("text/plain"), "");    
    Api apiInterface = (Api) Client.getApi();
    Call<AddPlayerMemberModel> call = apiInterface.getResult(id, name, type, fbody, gif);
    call.enqueue(new Callback<AddPlayerMemberModel>() {
                @Override
                public void onResponse(Call<AddPlayerMemberModel> call, Response<AddPlayerMemberModel> response) {
                    if (!response.isSuccessful()) {
                        return;
                    }
                    AddPlayerMemberModel model1 = response.body();
                    Toast.makeText(MainActivity.this, "Image Frame Successfully", Toast.LENGTH_SHORT).show();
                    if (model1.getStatus().equals("Image Upload SuccessFully")) {

                    }
                }
                @Override
                public void onFailure(Call<AddPlayerMemberModel> call, Throwable t) {
                }
            });
        }

    Here is the Interface class-
    public interface Api
    {
        @Multipart
        @POST("frame_image_save.php")
        Call<AddPlayerMemberModel> getResult(@Part("user_id") RequestBody user_id
                , @Part("imagename") RequestBody imagename
                , @Part("type")RequestBody type
                , @Part("image") MultipartBody.Part imageFile
                , @Part("gif_image")RequestBody gifimage
        );
    }

预期:成功显示 Toast 消息图像帧

实际结果:

原因:java.lang.IllegalArgumentException:使用 MultipartBody.Part 的 @Part 参数不得在注解中包含部件名称。 (参数 #4)用于方法 Api.getResult

【问题讨论】:

    标签: android retrofit2 image-file


    【解决方案1】:

    更新它....

    {
        @Multipart
        @POST("frame_image_save.php")
        Call<AddPlayerMemberModel> getResult(@Part("user_id") RequestBody user_id
                , @Part("imagename") RequestBody imagename
                , @Part("type")RequestBody type
                , @Part MultipartBody.Part imageFile
                , @Part("gif_image")RequestBody gifimage
        );
    }
    
    RequestBody mRequestBody  = RequestBody.create(MediaType.parse("image/*"), file)
    MultipartBody.Part fbody = MultipartBody.Part.createFormData("image", "filename.jpg", mRequestBody)
    

    【讨论】:

    • 谢谢它有效,我需要了解我们在哪里发送密钥,因为我的密钥是“图像”以及如何为图像发送空值?
    【解决方案2】:

    试试这个

    private void uploadFile(Uri fileUri) {
            File file = new File(fileUri.getPath());
            long sTime = System.currentTimeMillis();
            RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            MultipartBody.Part image = MultipartBody.Part.createFormData(sTime + "FramePlayer.Jpeg",file.getName(), requestFile);
            MultipartBody.Part id = MultipartBody.Part.createFormData("user_id", "1498");
            MultipartBody.Part type = MultipartBody.Part.createFormData("type", "player");
            MultipartBody.Part gif = MultipartBody.Part.createFormData("gif_image", "");
            Api apiInterface = (Api) Client.getApi();
            Call<AddPlayerMemberModel> call = apiInterface.getResult(id, image, type, gif);
            call.enqueue(new Callback<AddPlayerMemberModel>() {
                @Override
                public void onResponse(Call<AddPlayerMemberModel> call, Response<AddPlayerMemberModel> response) {
                    if (!response.isSuccessful()) {
                        return;
                    }
                    AddPlayerMemberModel model1 = response.body();
                    Toast.makeText(MainActivity.this, "Image Frame Successfully", Toast.LENGTH_SHORT).show();
                    if (model1.getStatus().equals("Image Upload SuccessFully")) {
    
                    }
                }
    
                @Override
                public void onFailure(Call<AddPlayerMemberModel> call, Throwable t) {
                }
            });
        }
    

    像这样改变

     @Multipart
            @POST("frame_image_save.php")
            Call<AddPlayerMemberModel> getResult(@Part MultipartBody.Part user_id,
                    @Part MultipartBody.Part imageFile,
                    @Part MultipartBody.Part type,
                    @Part MultipartBody.Part gifimage
                );
    

    【讨论】:

    • 无法访问我的 Api
    • @J Ramesh 这是正确的方法吗,因为我无法发送请求参数,这就是我在 onFailure() 方法上遇到问题的原因。
    猜你喜欢
    • 1970-01-01
    • 2015-07-16
    • 2015-07-27
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多