我有一个问题--我不知道为什么body返回null,这是我的模型。
package com.example.currencyapp.model;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable;
public class Rates implements Serializable {
@SerializedName("CAD")
@Expose
private String cad;
public Rates(String cad) {
this.cad = cad;
}
public Rates() {
}
public String getCad() {
return cad;
}
}这是我的json
{
"rates": {
"CAD": 1.5399,
}
}这是我的服务
import com.example.currencyapp.model.Rates;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface GetCurrencyDataService {
@GET("/latest")
Call<Rates> getCurrencyData();
} 和我的翻新实例
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitInstance {
private static Retrofit retrofit;
private static final String BASE_URL = "https://api.exchangeratesapi.io";
public static Retrofit getRetrofitInstance() {
if (retrofit == null) {
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}发布于 2020-07-22 03:52:50
你展示的JSON对象期望你的模型是:
public class CadObject implements Serializable {
@SerializedName("rates")
@Expose
private Rates rates;
...
class Rates implements Serializable {
@SerializedName("CAD")
@Expose
private String cad;
...
}
}这样做的原因是您有一个JSON对象,其中包含一个JSON对象,而JSON对象包含一个字符串值。
如果你想让你当前的模型工作,JSON对象结构应该是这样的:
{
"CAD": 1.5399
}https://stackoverflow.com/questions/63021929
复制相似问题