image upload using multipart retrofit 2

前端 未结 2 1314
无人共我
无人共我 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<ServerResponse> call = getResponse.uploadFile(fileToUpload1);
    Call<ServerResponse> 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.

    0 讨论(0)
  • 2021-01-25 03:13

    If your problem is that progress bar is not getting hidden, you are not calling progressDialog.dismiss(); in onFailure function.

    0 讨论(0)
提交回复
热议问题