Retrofit返回空数据

huangapple go评论52阅读模式
英文:

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(&quot;CAD&quot;)
    @Expose
    private String cad;

    public Rates(String cad) {
        this.cad = cad;
        
    }

    public Rates() {
    }

    public String getCad() {
        return cad;
    }

   
}

and here is my json

    {
        &quot;rates&quot;: {
            &quot;CAD&quot;: 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(&quot;/latest&quot;)
        Call&lt;Rates&gt; 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 = &quot;https://api.exchangeratesapi.io&quot;;
    
        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(&quot;rates&quot;)
    @Expose
    private Rates rates;

    ...

    class Rates implements Serializable {

        @SerializedName(&quot;CAD&quot;)
        @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:

{
    &quot;CAD&quot;: 1.5399
}

huangapple
  • 本文由 发表于 2020年7月22日 03:40:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63021929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定