image upload using multipart retrofit 2

前端 未结 2 1319
无人共我
无人共我 2021-01-25 02:35

Actually, I\'m new in this field.facing some problem during image upload.the process automatically suspended after some time.I am attaching my code below.Please anyone helps me

2条回答
  •  逝去的感伤
    2021-01-25 03:13

    You should upload your file like this

    File file1 = new File(mediaPath);
    int id=24;
    // File file2 = new File(mediaPath);
    
    RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file1);
    MultipartBody.Part body = MultipartBody.Part.createFormData("photo", file.getName(), requestFile);
    
    RequestBody requestBodyid = RequestBody.create(MediaType.parse("multipart/form-data"),id);
    
    
    ApiService getResponse = ApiClient.getClient().create(ApiService.class);
    // Call call = getResponse.uploadFile(fileToUpload1);
    Call call = getResponse.uploadFile(body,requestBodyid);
    

    instead of

    RequestBody requestBody1 = RequestBody.create(MediaType.parse("image/*"), file1);
    

    EDIT: You forgot to create request body for the id. Add these to the code.

    RequestBody requestBodyid = RequestBody.create(MediaType.parse("multipart/form-data"), id);
    

    Where id is your id, i.e id=24.

提交回复
热议问题