How to Pass the image in android using Retrofit?

后端 未结 6 570
一向
一向 2021-01-17 06:03

Hello i am working on upload image file using retrofit. Can any one have idea how to pass in

6条回答
  •  情话喂你
    2021-01-17 06:39

    /* Create interface like below. */
    
    public interface uploadWishImage {
    
        @Multipart
        @POST("upload/image")
        Call postImage(@Part MultipartBody.Part image, @Part("name") RequestBody name);
    }
    
    /* image upload code */
    
      File file = new File("here your file path");
            RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
            MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), reqFile);
            RequestBody name = RequestBody.create(MediaType.parse("text/plain"), "file");
            uploadWishImage postService = RetrofitApi.makeNetworkRequestWithHeaders(AddWish.this).create(uploadWishImage.class);
            Call call = postService.postImage(body, name);
            call.enqueue(new Callback() {
                @Override
                public void onResponse(Call call, Response response) {
                   // somethings to do with reponse
    
                }
    
                @Override
                public void onFailure(Call call, Throwable t) {
                    // Log error here since request failed
    
    
    
                }
            });
    

提交回复
热议问题