【问题标题】:Upload image file with Retrofit to database使用 Retrofit 将图像文件上传到数据库
【发布时间】:2019-08-02 22:26:27
【问题描述】:

我正在尝试使用 Retrofit 将图像上传到我的数据库,但到目前为止没有运气。在阅读了与此相关的所有线程并尝试了很多解决方案后,我决定发布我的问题。这是我在 Android 中的代码

API

@Multipart
@POST("createevent")
Call<JsonResponse> createEvent(@Field("title") String title,
                               @Part MultipartBody.Part imageFile,
                               @Field("description") String description,
                               @Field("id_type") int id_type,
                               @Part("image") RequestBody image,
                               @Field("id_group[1]") int id_group,
                               @Header("Authorization")String authHeader);

请求

public void CreateEvent(){

    keepAllDates();
    File file = new File("/storage/emulated/0/Download/carmena.jpg");
    RequestBody requestFile =
            RequestBody.create(MediaType.parse("multipart/form-data"), file);
    MultipartBody.Part body =
            MultipartBody.Part.createFormData("image", file.getName(), requestFile);
    RequestBody image =
            RequestBody.create(MediaType.parse("multipart/form-data"), "Your Name");


    Call<JsonResponse> peticion = api.createEvent (eventTile,body,eventDescription, idTypeEvent,image,id_group,tokenHc);
    peticion.enqueue(new Callback<JsonResponse>() {
        @Override
        public void onResponse(Call<JsonResponse> call, Response<JsonResponse> response) {

            int code = response.body().getCode();
            JsonResponse json = response.body();
            Log.d ( "Respuesta del servidor", response.body ().getMessage () );

            switch (code) {
                case 200:
                    String message = response.body ().getMessage ();

                    //listener.onGetEventsFinish ();

                    break;
                case 400:
                    // Toast.makeText ( MainActivity.this, errorMessage, Toast.LENGTH_SHORT ).show ();
                    String errorMessage = response.body ().getMessage ();
                    break;

                default:
                    //Toast.makeText ( MainActivity.this, errorMessage, Toast.LENGTH_SHORT ).show ();
                    String defaultmsg = response.body ().getMessage ();

            }

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

            Log.d ("Failure message", "fail");
            Log.d ("fail is", String.valueOf(t));

        }

    });
}

邮递员

我无法让它工作,它只是不上传任何东西。

【问题讨论】:

标签: android file-upload retrofit2


【解决方案1】:

如果您想发送带有请求的图像以及其他表单数据(如您附加的图像中的图像),使用 Retrofit 2.0 执行此操作的最简单方法是将其传递给 PartMapThis 链接有一个很好的教程,它基本上只是一个带有键值对的 Map 对象。

【讨论】:

  • 不错的教程,它可能会解决我的问题,谢谢!我在尝试实现它时遇到的一个问题,您将如何传递标头?因为在我的应用程序中,我需要通过标头传递令牌作为授权。 @Header 在这方面有用吗?
  • 是的,@Header 仍然有效,您可以像以前一样在方法参数中添加它。如果您想预先设置请求类型等内容,也可以使用方法注释来设置标头。
猜你喜欢
  • 2022-01-18
  • 2014-09-28
  • 2011-08-14
  • 1970-01-01
  • 1970-01-01
  • 2011-04-21
  • 2015-09-18
  • 2019-04-21
  • 2020-01-13
相关资源
最近更新 更多