使用翻新将图像文件上传到数据库

时间:2019-03-12 12:36:02

标签: android file-upload retrofit2

我正在尝试使用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));

        }

    });
}

邮递员

Postman request with all fields

我无法正常工作,只是没有上传任何东西。

0 个答案:

没有答案