我无法返回经过改造的列表

时间:2018-07-23 14:31:49

标签: android retrofit

我有一个返回答案列表的类。但我无法返回此列表。如果我只是返回没有List的类,则它可以工作。但是返回的是根据模型的列表:

"Status": true, 
"Mensagem": "Operação realizada com sucesso", 
"Data": {
  "Cotacoes": [
  {
    "quotation_id": 1001,  
    "transaction_id": 1001,  
    "creation_date": "2018-06-14T08:46:47.054966-03:00",
    .......
    "pecas": [  
    {  
        "id": 1,  
        "item_id": 0,
        ....
    }
    ]
}
]
}

我的班级报价:

public class QuotationVehicleSellerModel implements Serializable {

    private static final long serialVersionUID = 842387749350567455L;
    private int quotation_id;
    private int transaction_id;
    ....

    //get and sets ....

}

在下面的代码示例中,我将类型列表放入调用中。但这会返回以下错误:

  

java.lang.IllegalStateException:应该为BEGIN_ARRAY,但是   BEGIN_OBJECT在第1行第2列路径$

如果我只是简单地在调用中传递了该类,则可以成功返回,但不会返回列表。

如何返回此列表?谢谢! 请遵循代码:

public class ListaService {
    APIService serviceapi = APIRetrofit.getRetrofitClient().create(APIService.class);

    public void listItems(String token, Context context,
                          final APIService.ReturnFutureCallback<List<QuotationVehicleSellerModel>> callback) {

        //ProgressUtils.progressStart(dialogWait,manager,true);
        Call<List<QuotationVehicleSellerModel>> call = serviceapi.listaCotacao(token, "1845");
        call.enqueue(new Callback<List<QuotationVehicleSellerModel>>() {
            @Override
            public void onResponse(Call<List<QuotationVehicleSellerModel>> call, Response<List<QuotationVehicleSellerModel>> response) {
                if (response.isSuccessful()) {
                    Toast.makeText(context, "Sucesso", Toast.LENGTH_SHORT).show();
                    List<QuotationVehicleSellerModel> quotationVehicleSellerModel = response.body();
                    callback.onSuccess(quotationVehicleSellerModel);
                } else {
                    Toast.makeText(context, "Tristeza", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<List<QuotationVehicleSellerModel>> call, Throwable t) {
                Toast.makeText(context, "Tristeza onFailure", Toast.LENGTH_SHORT).show();
                Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

    }

}

接口:

@POST("api/cotacao/listar")
@FormUrlEncoded
Call<List<QuotationVehicleSellerModel>> listaCotacao(@Header("Authorization") String token, @Field("seller_company_id") String seller_company_id);

1 个答案:

答案 0 :(得分:1)

您的pojo类似乎是错误的,您无法直接访问内部列表,尝试先获取外部对象,然后再访问内部数组。准备如下三个模型类

JsonResponse.java

    public class JsonResponse {

@SerializedName("Status")
@Expose
private Boolean status;
@SerializedName("Mensagem")
@Expose
private String mensagem;
@SerializedName("Data")
@Expose
private Data data;

public Boolean getStatus() {
return status;
}

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

public String getMensagem() {
return mensagem;
}

public void setMensagem(String mensagem) {
this.mensagem = mensagem;
}

public Data getData() {
return data;
}

public void setData(Data data) {
this.data = data;
}

}

Data.java

    public class Data {

@SerializedName("Cotacoes")
@Expose
private List<Cotaco> cotacoes = null;

public List<Cotaco> getCotacoes() {
return cotacoes;
}

public void setCotacoes(List<Cotaco> cotacoes) {
this.cotacoes = cotacoes;
}

}

Cotaco.java

    public class Cotaco {

@SerializedName("quotation_id")
@Expose
private Integer quotationId;
@SerializedName("transaction_id")
@Expose
private Integer transactionId;
@SerializedName("creation_date")
@Expose
private String creationDate;
@SerializedName("pecas")
@Expose
private List<Peca> pecas = null;

public Integer getQuotationId() {
return quotationId;
}

public void setQuotationId(Integer quotationId) {
this.quotationId = quotationId;
}

public Integer getTransactionId() {
return transactionId;
}

public void setTransactionId(Integer transactionId) {
this.transactionId = transactionId;
}

public String getCreationDate() {
return creationDate;
}

public void setCreationDate(String creationDate) {
this.creationDate = creationDate;
}

public List<Peca> getPecas() {
return pecas;
}

public void setPecas(List<Peca> pecas) {
this.pecas = pecas;
}

}

现在让您的界面返回Callback<JsonResponse>。按如下所示更改改造调用

    public class ListaService {
    APIService serviceapi = APIRetrofit.getRetrofitClient().create(APIService.class);

    public void listItems(String token, Context context,
                          final APIService.ReturnFutureCallback<JsonResponse> callback) {

        //ProgressUtils.progressStart(dialogWait,manager,true);
        Call<JsonResponse> call = serviceapi.listaCotacao(token, "1845");
        call.enqueue(new Callback<JsonResponse>() {
            @Override
            public void onResponse(Call<JsonResponse> call, Response<JsonResponse> response) {
                if (response.isSuccessful()) {
                    Toast.makeText(context, "Sucesso", Toast.LENGTH_SHORT).show();
                    List<Cotaco> quotationVehicleSellerModel = response.body().getData().getCotacoes();
                    callback.onSuccess(quotationVehicleSellerModel);
                } else {
                    Toast.makeText(context, "Tristeza", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<JsonResponse> call, Throwable t) {
                Toast.makeText(context, "Tristeza onFailure", Toast.LENGTH_SHORT).show();
                Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

    }

}

界面

    @POST("api/cotacao/listar")
@FormUrlEncoded
Call<JsonResponse> listaCotacao(@Header("Authorization") String token, @Field("seller_company_id") String seller_company_id);