Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Sending complex data with image using Retrofit

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.17k
    Comment on it

    Let us suppose you have to create post request params in below mentioned format:

    Here I have to send a user object which include the picture parameter ,to achieve  this you have to create HashMap which include RequestBody as parameters and you need Multipartbody object. 

    {
        "user": {
            "email": "",
            "password": "",
            "name": "",
            "passcode": "",
            "picture": "",
            "currency_symbol_id": ""

        }
    }

     

    1. Create ApiInterface
      @Multipart
      @POST("/api/user/register")
      Call<ResponseBody> createUser(@PartMap() Map<String, RequestBody> requestBodyMap,@Part MultipartBody.Part file);
    2. Create MultipartBody and request Hashmap which are used in createUser:
      try {
          MultipartBody.Part imageFileBody=null;
          File fileToUpload=new File(mCurrentPhotoPath);
            RequestBody requestBody = RequestBody.create(MediaType.parse(Constants.CONTENT_IMAGE), fileToUpload);
        imageFileBody = MultipartBody.Part.createFormData(Constants.KEY_PIC, fileToUpload.getName(), requestBody);
              // call api to create new account
           Map<String,RequestBody> requestBodyMap=new HashMap<>();
          requestBodyMap.put(Constants.KEY_EMAIL, RequestBody.create(MediaType.parse(Constants.TEXT_PLAIN_TYPE),email));
          requestBodyMap.put(Constants.KEY_PASSWORD, RequestBody.create(MediaType.parse(Constants.TEXT_PLAIN_TYPE),password));
          requestBodyMap.put(Constants.KEY_PHONE, RequestBody.create(MediaType.parse(Constants.TEXT_PLAIN_TYPE),phoneNo));
          requestBodyMap.put(Constants.KEY_NAME, RequestBody.create(MediaType.parse(Constants.TEXT_PLAIN_TYPE),fullname));
          requestBodyMap.put(Constants.KEY_TIME_ZONE, RequestBody.create(MediaType.parse(Constants.TEXT_PLAIN_TYPE),Utils.getTimeZone()));
          requestBodyMap.put(Constants.KEY_UDID, RequestBody.create(MediaType.parse(Constants.TEXT_PLAIN_TYPE),notificationToken));
          requestBodyMap.put(Constants.KEY_TYPE, RequestBody.create(MediaType.parse(Constants.TEXT_PLAIN_TYPE),Constants.DEVICE_TYPE));
          requestBodyMap.put(Constants.KEY_CURRENCY, RequestBody.create(MediaType.parse(Constants.TEXT_PLAIN_TYPE),selectedCurrency.getId().toString()));
          
      }catch (Exception e){
      
      }

       // Note  : here  Constants.KEY_EMAIL name refers : "user[email]"

             

    public static final String IMAGE_TYPE="image/*";
    public static final String TEXT_PLAIN_TYPE="text/plain";
    public static final String CONTENT_IMAGE="image/jpeg";
    public static final String FB_PROVIDER="facebook";
    public static final String GOOGLE_PROVIDER="google";
    public static final String DEVICE_TYPE="ANDROID";

     

     

    Thanks ..

    Happy coding 

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: