Ciro González
Ciro González

Reputation: 367

Reading JSON - Retrofit - Android Studio

i really ned help with this. Im not being able to read the JSON and i dont know what im doing wrong.

I will drop my code here.

I have this Json

 {
    "id": "288",
    "name": "Tarjeta Shopping",
    "secure_thumbnail": "https://www.mercadopago.com/org-img/MP3/API/logos/288.gif",
    "thumbnail": "http://img.mlstatic.com/org-img/MP3/API/logos/288.gif",
    "processing_mode": "aggregator",
    "merchant_account_id": null
  }

This is my class that should represent that JSON

public class Tarjeta {

@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("secure_thumbnail")
@Expose
private String secureThumbnail;
@SerializedName("thumbnail")
@Expose
private String thumbnail;
@SerializedName("processing_mode")
@Expose
private String processingMode;
@SerializedName("merchant_account_id")
@Expose
private Object merchantAccountId;

public Tarjeta() {
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getSecureThumbnail() {
    return secureThumbnail;
}

public void setSecureThumbnail(String secureThumbnail) {
    this.secureThumbnail = secureThumbnail;
}

public String getThumbnail() {
    return thumbnail;
}

public void setThumbnail(String thumbnail) {
    this.thumbnail = thumbnail;
}

public String getProcessingMode() {
    return processingMode;
}

public void setProcessingMode(String processingMode) {
    this.processingMode = processingMode;
}

public Object getMerchantAccountId() {
    return merchantAccountId;
}

public void setMerchantAccountId(Object merchantAccountId) {
    this.merchantAccountId = merchantAccountId;
}

@Override
public String toString() {
    return "Tarjeta{" +
            "id='" + id + '\'' +
            ", name='" + name + '\'' +
            ", secureThumbnail='" + secureThumbnail + '\'' +
            ", thumbnail='" + thumbnail + '\'' +
            ", processingMode='" + processingMode + '\'' +
            ", merchantAccountId=" + merchantAccountId +
            '}';
}

}

this is my GET method

@GET("payment_methods/card_issuers")
Call<Tarjeta> getTarjetas2(@Query("public_key") String apiKey,
                           @Query("payment_method_id") String payment_method_id);

And this is where i try to read it.

botonTest2.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
      System.out.println("Test boton 2 clickeado");
      Retrofit retrofit = new Retrofit.Builder()
              .baseUrl(BASE_URL)
              .addConverterFactory(GsonConverterFactory.create())
              .build();

      ServicePago servicePago = retrofit.create(ServicePago.class);
      Call<Tarjeta> contenedorTarjetaCall = servicePago.getTarjetas2(apiKey,"visa");

      contenedorTarjetaCall.enqueue(new Callback<Tarjeta>() {
          @Override
          public void onResponse(Call<Tarjeta> call, Response<Tarjeta> response) {
              Toast.makeText(MainActivity.this, "BIEN", Toast.LENGTH_SHORT).show();

          }

          @Override
          public void onFailure(Call<Tarjeta> call, Throwable t) {
              Toast.makeText(MainActivity.this, "ALGO SALIO MAL", Toast.LENGTH_SHORT).show();
          }
      });
  }

});

Im habing this error: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

I think my class is correctly modelated, im completly lost.

Upvotes: 0

Views: 73

Answers (1)

seyed Jafari
seyed Jafari

Reputation: 1265

since you did not post your entire JSON I'm going to answer with a rough idea hope it helps.

the error states that the received JSON is not a Tarjeta but an array of Tarjeta. so to fix it I guess you just have to wrap your response in a list type. so it goes something like this:

@GET("payment_methods/card_issuers")
Call<List<Tarjeta>> getTarjetas2(@Query("public_key") String apiKey,
                           @Query("payment_method_id") String payment_method_id);

Upvotes: 1

Related Questions