Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

android - How to send PUT request with retrofit string and array list of model I need to use URL encoded

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...