【问题标题】:How to send POST request with data & List<CustomeList> in retrofit follow MVP pattern in android如何在改造中发送带有数据和 List<CustomeList> 的 POST 请求,遵循 android 中的 MVP 模式
【发布时间】:2020-04-04 18:05:13
【问题描述】:

我正在尝试发送带有数据的发布请求并尝试在请求中添加列表,但我不知道如何在请求中同时发送“数据和列表”,请帮助我。 这是我的要求。

{
    "wo_id": "0",
    "issue_branch": "AHMED01",
    "issue_date": "12-6-2019",
    "wo_type": "R",
    "vehicle_details": [
        {
            "short_close_qty": "0",
            "width": "8",
            "height": "8",
            "rfb_actual_allowed": "2"
        },
        {
            "short_close_qty": "0",
            "width": "8",
            "height": "8",
            "rfb_actual_allowed": "2"
        }
    ]
}

inMVP-createPresenterImpl 文件有正文:

private Map<String, String> getBody() {
        Map<String, String> params = new HashMap<>();
        params.put("wo_id", createWOView.getWoId());
        params.put("issue_branch ",createWOView.getIssueBranch());
        params.put("issue_date", createWOView.getIssueDate());
        params.put("status", createWOView.getStatus());
        params.put("wo_type",createWOView.getWOType());
        params.put("vehicle_details", new Gson().toJson(createWOView.getVehicleList())); //getting errer here

  }

//http://localhost:54171/api/WorkOrder/SaveWorkOrder @POST("WorkOrder/SaveWorkOrder") Observable createWoRequest(@HeaderMap Map headers, @Body Map body);

【问题讨论】:

  • 您是否遇到任何错误或异常?

标签: android post request retrofit


【解决方案1】:

根据您的要求创建模型

请求类:

import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;
import java.util.List;

public class ReqWorkOrder {

    @SerializedName("wo_type")
    private String woType;

    @SerializedName("wo_id")
    private String woId;

    @SerializedName("issue_date")
    private String issueDate;

    @SerializedName("issue_branch")
    private String issueBranch;

    @SerializedName("status")
    private String status;

    @SerializedName("vehicle_details")
    private List<Vehicle> vehicleDetails = new ArrayList<>();

    public void setWoType(String woType) {
        this.woType = woType;
    }

    public String getWoType() {
        return woType;
    }

    public void setWoId(String woId) {
        this.woId = woId;
    }

    public String getWoId() {
        return woId;
    }

    public void setIssueDate(String issueDate) {
        this.issueDate = issueDate;
    }

    public String getIssueDate() {
        return issueDate;
    }

    public void setIssueBranch(String issueBranch) {
        this.issueBranch = issueBranch;
    }

    public String getIssueBranch() {
        return issueBranch;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public List<Vehicle> getVehicleDetails() {
        return vehicleDetails;
    }

    public void setVehicleDetails(List<Vehicle> vehicleDetails) {
        this.vehicleDetails = vehicleDetails;
    }
}

车辆类别:

import com.google.gson.annotations.SerializedName;

public class Vehicle{

    @SerializedName("short_close_qty")
    private String shortCloseQty;

    @SerializedName("width")
    private String width;

    @SerializedName("rfb_actual_allowed")
    private String rfbActualAllowed;

    @SerializedName("height")
    private String height;

    public void setShortCloseQty(String shortCloseQty){
        this.shortCloseQty = shortCloseQty;
    }

    public String getShortCloseQty(){
        return shortCloseQty;
    }

    public void setWidth(String width){
        this.width = width;
    }

    public String getWidth(){
        return width;
    }

    public void setRfbActualAllowed(String rfbActualAllowed){
        this.rfbActualAllowed = rfbActualAllowed;
    }

    public String getRfbActualAllowed(){
        return rfbActualAllowed;
    }

    public void setHeight(String height){
        this.height = height;
    }

    public String getHeight(){
        return height;
    }
}

更新您的getBody()

 private ReqWorkOrder getBody(){
        ReqWorkOrder reqWorkOrder=new ReqWorkOrder();
        reqWorkOrder.setWoId(createWOView.getWoId());
        reqWorkOrder.setIssueBranch(createWOView.getIssueBranch());
        reqWorkOrder.setIssueDate(createWOView.getIssueDate());
        reqWorkOrder.setWoType(createWOView.getWOType());
        reqWorkOrder.setStatus(createWOView.getStatus());
        reqWorkOrder.getVehicleDetails().addAll(createWOView.getVehicleList());

        return reqWorkOrder;
    }

你的api请求方法

@POST("WorkOrder/SaveWorkOrder")
Competable createWoRequest(@HeaderMap Map headers, @Body ReqWorkOrder body);

仅供参考:Competable 不会返回任何内容。

希望对你有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 2019-10-09
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多