问题
can anyone guide me how to send PUT request with this json
{
"delivery_status": "Partially Completed",
"signatures": "==skdjfkjdsakjhfoiuewyrdskjhfjdsaf",
"assignee_note": "this is remarks and and and nothing",
"id": "this is remarks and and and nothing",
"returned_products": [
{
"id": "18",
"quantity": 3,
"reasons": "i dont know reason .. bus wapis a gya saman :-)"
},
{
"id": "19",
"quantity": 4,
"reasons": "i dont know reason .. bus wapis a gya saman :-)"
}
]
}
here is what i have tried but failed
@FormUrlEncoded
@PUT("delivery_notes/update/1.json")
Call<UploadDeliveryNote> postDeliveryNote(
@Field("returned_products[]") ArrayList<ReturnedProduct> returned_products,
@Field("delivery_status") String deliveryStatus,
@Field("signatures") String signatures,
@Field("id") String id,
@Field("assignee_note") String note
);
but failed then tried this.
@Headers("Content-Type: application/json")
@PUT("delivery_notes/update/1.json")
Call<UploadDeliveryNote> postDeliveryNote(@Body String body);
what am i doing wrong in this? my main problem is i am sending simple strings and one object of model is a list of models returned_products Thanks in advance.
回答1:
There can be many ways to implement this call via Retrofit, the most easy I can think is to make model classes.
Your call will look like-
@PUT(""delivery_notes/update/1.json"")
Call<ApiResponse<UploadDeliveryNote>> postDeliveryNote(@Body Example example);
and call it like
Example example = new Example();
example.setAssigneeNote();
example.setDeliveryStatus();
example.setReturnedProducts();
apiInterface.postDeliveryNote(example);
Example.java
public class Example {
@SerializedName("delivery_status")
@Expose
private String deliveryStatus;
@SerializedName("signatures")
@Expose
private String signatures;
@SerializedName("assignee_note")
@Expose
private String assigneeNote;
@SerializedName("id")
@Expose
private String id;
@SerializedName("returned_products")
@Expose
private List<ReturnedProduct> returnedProducts = null;
public String getDeliveryStatus() {
return deliveryStatus;
}
public void setDeliveryStatus(String deliveryStatus) {
this.deliveryStatus = deliveryStatus;
}
public String getSignatures() {
return signatures;
}
public void setSignatures(String signatures) {
this.signatures = signatures;
}
public String getAssigneeNote() {
return assigneeNote;
}
public void setAssigneeNote(String assigneeNote) {
this.assigneeNote = assigneeNote;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<ReturnedProduct> getReturnedProducts() {
return returnedProducts;
}
public void setReturnedProducts(List<ReturnedProduct> returnedProducts) {
this.returnedProducts = returnedProducts;
}
}
ReturnedProduct.java
public class ReturnedProduct {
@SerializedName("id")
@Expose
private String id;
@SerializedName("quantity")
@Expose
private int quantity;
@SerializedName("reasons")
@Expose
private String reasons;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public String getReasons() {
return reasons;
}
public void setReasons(String reasons) {
this.reasons = reasons;
}
}
来源:https://stackoverflow.com/questions/52368459/how-to-send-put-request-with-retrofit-string-and-array-list-of-model-i-need-to-u