这件事我真的需要帮助。我不能阅读JSON,我不知道我做错了什么。
我将把我的代码放在这里。
我有这个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
}这是我应该表示该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 +
'}';
}}
这是我的GET方法
@GET("payment_methods/card_issuers")
Call<Tarjeta> getTarjetas2(@Query("public_key") String apiKey,
@Query("payment_method_id") String payment_method_id);这就是我尝试阅读它的地方。
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();
}
});
}});
我遇到此错误: java.lang.IllegalStateException:预期的BEGIN_OBJECT,但在第1行第2列路径$处为BEGIN_ARRAY
我认为我的类是正确建模的,我完全迷失了。
发布于 2018-12-22 01:43:39
既然你没有发布你的整个JSON,我将用一个粗略的想法来回答,希望它能有所帮助。
该错误指出接收到的JSON不是Tarjeta,而是Tarjeta数组。所以要解决这个问题,我想你只需要把你的响应包装在一个列表类型中。所以它是这样的:
@GET("payment_methods/card_issuers")
Call<List<Tarjeta>> getTarjetas2(@Query("public_key") String apiKey,
@Query("payment_method_id") String payment_method_id);https://stackoverflow.com/questions/53888578
复制相似问题