【问题标题】:Retrofit sending Multipart and Form data in single request改造在单个请求中发送 Multipart 和 Form 数据
【发布时间】:2015-09-01 15:06:21
【问题描述】:

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

    @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 还有其他方法吗?

【问题讨论】:

  • 你添加@Multipart注解了吗
  • @PramodYadav ,是的,我尝试过,但出现错误,即只能使用 Multipart 或 FormUrlEncoded 两者都是不允许的。如果我使用了 Multipart,那么如何将这些工作 FormUrlEncoded 数据转换为 Multipart..?
  • 检查this
  • @blipinsk 我已经检查了您提供的上述链接,但是我怎样才能在此请求中添加图像文件..?
  • 关于这个问题的任何更新..??

标签: android image retrofit multipartform-data


【解决方案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);

}

【讨论】:

  • 感谢您的回复,但我怎样才能在您的代码中包含字符串电话号码数组..?而在这一部分中, TypedString userName = new TypedString("userName");我应该在“userName”中使用我的用户名变量吗?它让我对 Part("userName")... 感到困惑 :)
  • Part("UserName") 是您的请求对象以及您的文件对象。您需要使用“TypedFile”发送文件对象,其余对象需要使用“TypedString”发送
  • 我已经尝试过你的代码,但你可以从我上面的代码中看到我需要发送一个字符串数组来发送这个请求。请看我的代码 TypedString groupId = new TypedString(mGroupJid); TypedString 标识 = new TypedString(mGroupNameText); TypedString[] 数组 = 新的 TypedString[50];整数 j = 0; for(String jid : mMembersList){ TypedString str = new TypedString(jid);数组[j] = str; j++; Log.e("TAg","....... 数组 "+array[j]+" str "+str+" jid "+jid);它没有醒来.....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-30
  • 2014-05-04
  • 1970-01-01
  • 2019-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多