Reputation:
I have a class that returns a list in the answer. But I can not return this list. If, I simply return the class, without a List, it works. But the return, is a list according to the model:
"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,
....
}
]
}
]
}
My class Quotation:
public class QuotationVehicleSellerModel implements Serializable {
private static final long serialVersionUID = 842387749350567455L;
private int quotation_id;
private int transaction_id;
....
//get and sets ....
}
In the code example below, I put the type list in the call. But it returns me the following error:
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
If I simply pass the class on the call, I get the return successfully, but it does not return me a list.
How to return this list? Thank you! Follow the code:
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();
}
});
}
}
Interface:
@POST("api/cotacao/listar")
@FormUrlEncoded
Call<List<QuotationVehicleSellerModel>> listaCotacao(@Header("Authorization") String token, @Field("seller_company_id") String seller_company_id);
Upvotes: 0
Views: 401
Reputation: 5017
Your pojo classes seems to be wrong, you cant directly access the inner list,try to first fetch the outer object and then access the inner array. Prepare three model classes like below
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;
}
}
Now make your interface to return Callback<JsonResponse>
.Change the retrofit call like this
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();
}
});
}
}
interface
@POST("api/cotacao/listar")
@FormUrlEncoded
Call<JsonResponse> listaCotacao(@Header("Authorization") String token, @Field("seller_company_id") String seller_company_id);
Upvotes: 1