gpt4 book ai didi

java - 读取 JSON - Retrofit - Android Studio

转载 作者:行者123 更新时间:2023-12-02 10:26:53 25 4
gpt4 key购买 nike

我真的需要帮助。我无法读取 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

我认为我的类(class)建模正确,但我完全迷失了。

最佳答案

由于您没有发布完整的 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);

关于java - 读取 JSON - Retrofit - Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53888578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com