改造响应体null

时间:2018-05-25 12:15:05

标签: android post request retrofit2

当我发布请求响应正文为空但状态为200时?

我做错了什么?

这是我的通话方式

  private void getTransferBtwCenterAndOffice(){

    showProgressDialog(getString(R.string.wait_message));
    ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
    String userName = tinyDb.getString(Constant.userName);
    String passWord = tinyDb.getString(Constant.token);
    List<TransferCenterStoreParameters> parameters = new ArrayList<>();
    parameters.add(new TransferCenterStoreParameters("StoreCode","B201"));
    String base = userName + ":" + passWord;
    String authHeader = "Basic "+ Base64.encodeToString(base.getBytes(),Base64.NO_WRAP);
    Call<TransferCenterStoreResponse> call = apiService.TRANSFER_CENTER_STORE_RESPONSE_CALL(authHeader,"BR_SP_BRN_GET_STORE_ACTIVE_SHIPMENTS",parameters);

    call.enqueue(new Callback<TransferCenterStoreResponse>() {
        @Override
        public void onResponse(Call<TransferCenterStoreResponse> call, retrofit2.Response<TransferCenterStoreResponse> response) {
            hideProgressDialog();
            Log.d(CLASS_NAME, "onResponseeeeeeeee: "+response);
            if(response != null && response.body() != null){
                if(response.body().getTransferBtwCenterStoreResult().getResultCode().equals("200")){
                    List<TransferCenterStore> mList = response.body().getTransferCenterStoresList();
                }
                else{
                    getAlert(response.body().getTransferBtwCenterStoreResult().getErrorMessage());
                }
            }
        }

        @Override
        public void onFailure(Call<TransferCenterStoreResponse> call, Throwable t) {
            hideProgressDialog();
            Log.e(CLASS_NAME, t.toString());
        }
    });
}

这是我的apiinterface

@FormUrlEncoded
    @POST("GetDispatchList")
    Call<TransferCenterStoreResponse> TRANSFER_CENTER_STORE_RESPONSE_CALL(
            @Header("Authorization") String authHeader,
            @Field("ProcName") String procName,
            @Field("Parameters") List<TransferCenterStoreParameters> objects) ;

这是我的参数主体

  

{                   “ProcName”:“BR_SP_BRN_GET_STORE_ACTIVE_SHIPMENTS”,                   “参数”:[       {         “名称”:“StoreCode”,         “价值”:“B201”       }]}

这是我的transfercenterstoreresponse类

public class TransferCenterStoreResponse {

    @SerializedName("ResultModel")
    private TransferBtwCenterStoreResult transferBtwCenterStoreResult;

    @SerializedName("Dispatches")
    private List<TransferCenterStore> transferCenterStoresList;


    public TransferBtwCenterStoreResult getTransferBtwCenterStoreResult() {
        return transferBtwCenterStoreResult;
    }

    public void setTransferBtwCenterStoreResult(TransferBtwCenterStoreResult transferBtwCenterStoreResult) {
        this.transferBtwCenterStoreResult = transferBtwCenterStoreResult;
    }

    public List<TransferCenterStore> getTransferCenterStoresList() {
        return transferCenterStoresList;
    }

    public void setTransferCenterStoresList(List<TransferCenterStore> transferCenterStoresList) {
        this.transferCenterStoresList = transferCenterStoresList;
    }
}

这是我的回复

{
    "ResultModel": {
        "ResultStatus": "Success",
        "ResultCode": "200",
        "ErrorMessage": null,
        "Token": "BD0F1E3D1F8E43B79BCE96FA6572A2AD",
        "ResultJson": "",
        "ConnectionUrl": "http://192.168.3.40:1313/",
        "UserName": null,
        "ResultNative": "",
        "ResultDt": null
    },
    "Dispatches": [
        {
            "ShippingNumber": "178-S-6-30770",
            "ShippingDate": "2018-02-11T21:00:00Z",
            "Description": "",
            "InternalDescription": "",
            "SeriesNumber": 322216,
            "LogisticsCompanyBOL": "",
            "WarehouseCode": "B456",
            "ShipmentHeaderID": "3408fcec-9e8f-4e82-b556-a88500d3713f",
            "Quantitity": 1
        }
]
}

1 个答案:

答案 0 :(得分:0)

如果您从服务器获得200并获取json数据,那么请确保您的响应pojo类与服务器给出的json密钥相同。

检查下面的pojo课程。

public class DispatchesItem{
private String description;
private int quantitity;
private String shippingNumber;
private String shippingDate;
private int seriesNumber;
private String shipmentHeaderID;
private String internalDescription;
private String warehouseCode;
private String logisticsCompanyBOL;

public void setDescription(String description){
    this.description = description;
}

public String getDescription(){
    return description;
}

public void setQuantitity(int quantitity){
    this.quantitity = quantitity;
}

public int getQuantitity(){
    return quantitity;
}

public void setShippingNumber(String shippingNumber){
    this.shippingNumber = shippingNumber;
}

public String getShippingNumber(){
    return shippingNumber;
}

public void setShippingDate(String shippingDate){
    this.shippingDate = shippingDate;
}

public String getShippingDate(){
    return shippingDate;
}

public void setSeriesNumber(int seriesNumber){
    this.seriesNumber = seriesNumber;
}

public int getSeriesNumber(){
    return seriesNumber;
}

public void setShipmentHeaderID(String shipmentHeaderID){
    this.shipmentHeaderID = shipmentHeaderID;
}

public String getShipmentHeaderID(){
    return shipmentHeaderID;
}

public void setInternalDescription(String internalDescription){
    this.internalDescription = internalDescription;
}

public String getInternalDescription(){
    return internalDescription;
}

public void setWarehouseCode(String warehouseCode){
    this.warehouseCode = warehouseCode;
}

public String getWarehouseCode(){
    return warehouseCode;
}

public void setLogisticsCompanyBOL(String logisticsCompanyBOL){
    this.logisticsCompanyBOL = logisticsCompanyBOL;
}

public String getLogisticsCompanyBOL(){
    return logisticsCompanyBOL;
}

@Override
public String toString(){
    return 
        "DispatchesItem{" + 
        "description = '" + description + '\'' + 
        ",quantitity = '" + quantitity + '\'' + 
        ",shippingNumber = '" + shippingNumber + '\'' + 
        ",shippingDate = '" + shippingDate + '\'' + 
        ",seriesNumber = '" + seriesNumber + '\'' + 
        ",shipmentHeaderID = '" + shipmentHeaderID + '\'' + 
        ",internalDescription = '" + internalDescription + '\'' + 
        ",warehouseCode = '" + warehouseCode + '\'' + 
        ",logisticsCompanyBOL = '" + logisticsCompanyBOL + '\'' + 
        "}";
    }
}


public class ResultModel{
private String resultJson;
private Object userName;
private String resultStatus;
private String connectionUrl;
private String token;
private String resultNative;
private Object resultDt;
private Object errorMessage;
private String resultCode;

public void setResultJson(String resultJson){
    this.resultJson = resultJson;
}

public String getResultJson(){
    return resultJson;
}

public void setUserName(Object userName){
    this.userName = userName;
}

public Object getUserName(){
    return userName;
}

public void setResultStatus(String resultStatus){
    this.resultStatus = resultStatus;
}

public String getResultStatus(){
    return resultStatus;
}

public void setConnectionUrl(String connectionUrl){
    this.connectionUrl = connectionUrl;
}

public String getConnectionUrl(){
    return connectionUrl;
}

public void setToken(String token){
    this.token = token;
}

public String getToken(){
    return token;
}

public void setResultNative(String resultNative){
    this.resultNative = resultNative;
}

public String getResultNative(){
    return resultNative;
}

public void setResultDt(Object resultDt){
    this.resultDt = resultDt;
}

public Object getResultDt(){
    return resultDt;
}

public void setErrorMessage(Object errorMessage){
    this.errorMessage = errorMessage;
}

public Object getErrorMessage(){
    return errorMessage;
}

public void setResultCode(String resultCode){
    this.resultCode = resultCode;
}

public String getResultCode(){
    return resultCode;
}

@Override
public String toString(){
    return 
        "ResultModel{" + 
        "resultJson = '" + resultJson + '\'' + 
        ",userName = '" + userName + '\'' + 
        ",resultStatus = '" + resultStatus + '\'' + 
        ",connectionUrl = '" + connectionUrl + '\'' + 
        ",token = '" + token + '\'' + 
        ",resultNative = '" + resultNative + '\'' + 
        ",resultDt = '" + resultDt + '\'' + 
        ",errorMessage = '" + errorMessage + '\'' + 
        ",resultCode = '" + resultCode + '\'' + 
        "}";
    }
  }

和你的回复课

public class Response{
@SerializedName("Dispatches")
private List<DispatchesItem> dispatches;
@SerializedName("ResultModel")
private ResultModel resultModel;

public void setDispatches(List<DispatchesItem> dispatches){
    this.dispatches = dispatches;
}

public List<DispatchesItem> getDispatches(){
    return dispatches;
}

public void setResultModel(ResultModel resultModel){
    this.resultModel = resultModel;
}

public ResultModel getResultModel(){
    return resultModel;
}

@Override
public String toString(){
    return 
        "Response{" + 
        "dispatches = '" + dispatches + '\'' + 
        ",resultModel = '" + resultModel + '\'' + 
        "}";
    }

}

将所有pojo类字段放在@SerializedName(“json key”)上方的json键中。