英文:
Retrofit returns null data
问题
我遇到了一个问题 - 我不知道为什么这里的主体返回为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();
}
这是我的Retrofit实例
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;
}
}
英文:
I'm having an issue - I don't know why the body returns null here is my model.
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;
}
}
and here is my json
{
"rates": {
"CAD": 1.5399,
}
}
and this is my service
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();
}
and my retrofit instance
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;
}
}
答案1
得分: 1
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对象结构应如下所示:
{
"CAD": 1.5399
}
英文:
JSON object you presented expects your model to be:
public class CadObject implements Serializable {
@SerializedName("rates")
@Expose
private Rates rates;
...
class Rates implements Serializable {
@SerializedName("CAD")
@Expose
private String cad;
...
}
}
The reason for this is that you have a JSON object which holds a JSON object which holds a string value.
If you want your current model to work JSON object structure should look like this:
{
"CAD": 1.5399
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论