1

I need to send a request to the API using Retrofit 2. I want the request to look like this form-data in Postman.

Postman example enter image description here

I have an ArrayList and an image to upload. so I create this class "CreateGroupCard"

public class  CreateGroupCard implements Serializable
    {

        @SerializedName("userID")
        String userID;
        @SerializedName("group_name")
        String group_name;
        @SerializedName("new_name")
        String new_name;
        @SerializedName("group_id")
        int group_id;
        @SerializedName("group_members")
        List<sendMember> group_members;

        RequestBody photo;

        public CreateGroupCard() {
        }

        private CreateGroupCard(String userID, String group_name, List<sendMember> group_members, RequestBody photo) {
            this.userID = userID;
            this.group_name = group_name;
            this.group_members = group_members;
            this.photo = photo;
        } 
}

here is the interface

@POST(createGroupCard)
Call<CreatGroupCardApiResponse> createGroupCard
   (@Header("Authorization") String authorization, @Body CreateGroupCard CreateGroupCard);

here is my API call

File file = new File(getRealPathFromURI(card.getPhotoUri()));
RequestBody fileReqBody = RequestBody.create(MediaType.parse("image/jpeg"), file);
RequestBody requestBody = new MultipartBody.Builder()
                        .setType(MultipartBody.FORM)
                        .addFormDataPart("photo",file.getName(),fileReqBody)
                        .build();
CreateGroupCard  groupCard = new CreateGroupCard(user_id,card.getName(),members,requestBody);

ApiInterface apiInterface = retrofit.create(ApiInterface.class);
Call<CreatGroupCardApiResponse> call = apiInterface.createGroupCard("Bearer " + ACCESS_TOKEN,groupCard);

the getRealPathFromURI method

public String getRealPathFromURI(Uri contentUri)
    {
        String[] proj = {MediaStore.Images.Media.DATA};
        CursorLoader loader = new CursorLoader(this, contentUri, proj, null, null, null);
        Cursor cursor = loader.loadInBackground();
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String result = cursor.getString(column_index);
        cursor.close();
        return result;
    }

Take the photo and get the image

    private void openCamera()
    {


        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, TAKE_PICTURE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) 
   {
      super.onActivityResult(requestCode, resultCode, data);

      ImageView imageView = (ImageView) 
          viewPager.findViewWithTag("groupPhoto"+viewPager.getCurrentItem());



      if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK)
        {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            imageView.setImageBitmap(imageBitmap);
            Uri uri = getImageUri(getApplicationContext(), imageBitmap);


            Log.e("URI",uri.toString());
            groupCards.get(viewPager.getCurrentItem()).setPhotoUri(uri);

        }
    }

But the image is not uploaded, what is the mistake I did.

Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
zahraa
  • 13
  • 3
  • Does this answer your question? [POST Multipart Form Data using Retrofit 2.0 including image](https://stackoverflow.com/questions/34562950/post-multipart-form-data-using-retrofit-2-0-including-image) – KuLdip PaTel Jan 07 '20 at 07:20
  • You have to use multipart in retrofit to sent files – Vish Jan 07 '20 at 07:34

1 Answers1

0

Since from postman I can see you are sending the data as form data so you can do like this:

@POST("<your endurl>")
    Call<CreatGroupCardApiResponse> createGroupCard(@Header("Authorization") String authorization,@PartMap HashMap<String, RequestBody> partmap,@Part MultipartBody.Part part);

Create a hashmap of type RequestBody:

HashMap<String, RequestBody> partBodyMap = new HashMap()
Debasish Ghosh
  • 1,867
  • 20
  • 29