在单个请求中重新发送Multipart和Form数据

时间:2015-09-01 06:48:05

标签: android image retrofit multipartform-data

在我的应用程序中,我需要使用改装向服务器发送图像和电话号码数组以及一些独特的值。这是我使用的代码,如果我从请求中删除了图像,则以下代码正常工作。

    @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);

现在我需要在此请求中发送图像数据,但是如何在同一请求中同时使用FormUrlEncoded和多部分数据......? Retrofit还有其他方法吗?

1 个答案:

答案 0 :(得分:1)

请检查我的代码,希望它能为您提供帮助

    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);

}
相关问题