[英]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.
我无法读取JSON,而且我不知道自己在做什么错。
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 这是我的班级,应该代表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方法
@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 $ 我正在处理此错误:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在行1列2路径$处为BEGIN_ARRAY
I think my class is correctly modelated, im completly lost. 我认为我的课程建模正确,完全失去了学习机会。
since you did not post your entire JSON I'm going to answer with a rough idea hope it helps. 由于您没有发布整个JSON,因此我将给出一个大概的想法,希望对您有所帮助。
the error states that the received JSON is not a Tarjeta but an array of Tarjeta. 错误指出收到的JSON不是Tarjeta,而是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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.