Nidheesh
Nidheesh

Reputation: 443

Retrofit sending Multipart and Form data in single request

In my application i need to send an image and array of phone numbers and some unique values to server using retrofit. Here is the code that i have been used, the following code is working if i have removed image from request.

    @FormUrlEncoded
    @POST("/groups")
    @Headers("Accept:application/json")
    void createGroupRequest(@Header("mobile-number") String mPhone, @Header("uid") String imei,@Field("group[identification_name]") String jid, @Field("group[name]") String mName,@Field("group[mobile_numbers][]") String[] mMemberNos, Callback<RetrofitResponse> response);

Now i need to send an image data in this request, but how it possible to use both FormUrlEncoded and a multipart data in same request...? is there any other approach in Retrofit..?

Upvotes: 0

Views: 3318

Answers (1)

user4571931
user4571931

Reputation:

Please check my code hope it will help you

    private RestAdapter     adapter;
    private ApiListener     apis;

    adapter = new RestAdapter.Builder().setLogLevel(RestAdapter.LogLevel.FULL).setEndpoint(BASE_URL).build();
    apis = adapter.create(ApiListener.class);

TypedString userName = new TypedString("userName");
TypedString name = new TypedString("name");
TypedString emailAddress = new TypedString("emailAddress");
TypedString password = new TypedString("password");

File photoFile = new File(Environment.getExternalStorageDirectory().getPath()+ File.separator+"Koala.jpg");
TypedFile photoTypedFile = new TypedFile("image/*", photoFile);
apis.registerUser(userName,name,emailAddress,password,photoTypedFile, new Callback<BaseResponseVo>()
    {
        @Override
        public void failure(RetrofitError arg0)
        {
            progress.setVisibility(View.INVISIBLE);
        }
        @Override
        public void success(BaseResponseVo arg0, Response arg1)
        {
            progress.setVisibility(View.INVISIBLE);
        }
    });



public interface ApiListener
{
@Multipart
@POST("/user/add")
public void registerUser(@Part("userName") TypedString username,@Part("name") TypedString name,@Part("emailAddress") TypedString email,@Part("password") TypedString password,@Part("userPhotoURL") TypedFile photo,Callback<BaseResponseVo> response);

}

Upvotes: 1

Related Questions