Reputation: 59
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;
}
}
Upvotes: 0
Views: 57
Reputation: 4694
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
}
Upvotes: 1