传递自定义对象数组作为 Retrofit PUT 请求中的参数

huangapple go评论85阅读模式
英文:

Pass Custom array of Object as a Parameter in API Retrofit PUT Call

问题

我必须调用一个API来更新特定用户的紧急联系人。

https://stay-safe-api.azurewebsites.net/5eadd53761***0f6017/update/emergency_contact_details

//API的请求体
{
    "EmergencyContacts": [
        {
            "FullName": "Johon",
            "PhoneNumberCountryCode": "ae",
            "PhoneNumberCountryDialCode": "971",
            "PhoneNumber": "5534566"
        }
    ]
}

所以我有一个数组,我在API调用中传递它,但无论何时调用它,我都会收到一个错误,指出输入不正确。

我不知道我做错了什么,我实际上是新手,所以你们能帮我吗?

APIInterface.class

@FormUrlEncoded
@PUT("update/emergency_contact_details")
Call<RegisterResponse> setEmergencyContact(@Field("EmergencyContacts[]") List<EmergencyList> 
EmergencyContacts);

APIClient.class

public static Retrofit getClientEmerg(String id) {
    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl("https://stay-safe-api.azurewebsites.net" + id + "/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}

MainActivity.class

ApiInterface apiInterface = ApiClient.getClientEmerg(id).create(ApiInterface.class);
Call<RegisterResponse> call = apiInterface.setEmergencyContact(emergencyLists);
call.enqueue(new Callback<RegisterResponse>() {
    @Override
    public void onResponse(Call<RegisterResponse> call, Response<RegisterResponse> response) {
    
    }
    
    @Override
    public void onFailure(Call<RegisterResponse> call, Throwable t) {
        progress.dismiss();
    }
});

所以请告诉我,在API调用的参数中传递数组方面我做错了什么?因为我收到错误消息:"这是无效的请求。请确保输入正确。"

谢谢。

英文:

I have to call an API to update the emergency contact of specific user.

https://stay-safe-api.azurewebsites.net/5eadd53761***0f6017/update/emergency_contact_details

//Body of API  
{
&quot;EmergencyContacts&quot;: [
	{
		&quot;FullName&quot; : &quot;Johon&quot;,
		&quot;PhoneNumberCountryCode&quot; : &quot;ae&quot;,
		&quot;PhoneNumberCountryDialCode&quot; : &quot;971&quot;,
		&quot;PhoneNumber&quot; : &quot;5534566&quot;
		
	}	
]
}

So I have an array which is I am passing in my API Call but whenever it's called, I got an error that inputs are incorrect.

I dont know what I am doing wrong I am actually new so can you guys help me out ?

APIInteface.class

@FormUrlEncoded
@PUT(&quot;update/emergency_contact_details&quot;)
Call&lt;RegisterResponse&gt; setEmergencyContact(@Field(&quot;EmergencyContacts[]&quot;) List&lt;EmergencyList&gt; 
EmergencyContacts);

APIClient.class

 public static Retrofit getClientEmerg(String id) {
    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(&quot;https://stay-safe-api.azurewebsites.net&quot; + id + &quot;/&quot;)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}

MainActivity.class

 ApiInterface apiInterface = ApiClient.getClientEmerg(id).create(ApiInterface.class);
    Call&lt;RegisterResponse&gt; call = apiInterface.setEmergencyContact(emergencyLists);
    call.enqueue(new Callback&lt;RegisterResponse&gt;() {
        @Override
        public void onResponse(Call&lt;RegisterResponse&gt; call, Response&lt;RegisterResponse&gt; response) {

         }
        @Override
        public void onFailure(Call&lt;RegisterResponse&gt; call, Throwable t) {
            progress.dismiss();
        }
    });


}

So let me know that what I am doing wrong in passing array as a param in API Call? Because I am getting error that "This is invalid request. Please make sure inputs are correct."

Thanks.

答案1

得分: 0

尝试在您的ApiInterface中使用Body注释,并且移除FormUrlEncoded:

@PUT("update/emergency_contact_details")
Call<RegisterResponse> setEmergencyContact(@Body List<EmergencyList> EmergencyContacts);

不过奇怪的是,为什么您需要发送联系人列表,尽管似乎端点在路径中包含id(因此您可以通过此PUT仅更新一个联系人)。

英文:

Try to use Body annotation in your ApiInterface instead (and get rid of FormUrlEncoded):

@PUT(&quot;update/emergency_contact_details&quot;)
Call&lt;RegisterResponse&gt; setEmergencyContact(@Body List&lt;EmergencyList&gt; EmergencyContacts);

Still it's strange why you need to send contacts' list though it seemed that endpoint includes id in path (so you can update by this PUT just one contact)

huangapple
  • 本文由 发表于 2020年5月5日 21:44:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/61614659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定