英文:
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
{
"EmergencyContacts": [
{
"FullName" : "Johon",
"PhoneNumberCountryCode" : "ae",
"PhoneNumberCountryDialCode" : "971",
"PhoneNumber" : "5534566"
}
]
}
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("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();
}
});
}
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("update/emergency_contact_details")
Call<RegisterResponse> setEmergencyContact(@Body List<EmergencyList> 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论