Android Retrofit在对象上返回null

时间:2017-05-16 17:24:43

标签: android retrofit retrofit2

当我使用简单的api时:http://api.nbp.pl/api/exchangerates/rates/a/chf/ 并希望获得MID值,我总是有0.0作为回报。 我使用Json generatator生成2个类:(我尝试添加Serializable名称,但它不能解决我的问题)

public class Rater
{
    private String no;

    public String getNo() { return this.no; }

    public void setNo(String no) { this.no = no; }

    private String effectiveDate;

    public String getEffectiveDate() { return this.effectiveDate; }

    public void setEffectiveDate(String effectiveDate) { this.effectiveDate = effectiveDate; }

    private double mid;

    public double getMid() { return this.mid; }

    public void setMid(double mid) { this.mid = mid; }
}

public class RootObject
{
    private String table;

    public String getTable() { return this.table; }

    public void setTable(String table) { this.table = table; }

    private String currency;

    public String getCurrency() { return this.currency; }

    public void setCurrency(String currency) { this.currency = currency; }

    private String code;

    public String getCode() { return this.code; }

    public void setCode(String code) { this.code = code; }

    private ArrayList<Rater> raters;

    public ArrayList<Rater> getRaters() { return this.raters; }

    public void setRaters(ArrayList<Rater> raters) { this.raters = raters; }
}

简单的界面:

public interface RaterAPI {

    String baseUrl = "http://api.nbp.pl/";

        @GET("api/exchangerates/rates/a/chf/")Call<Rater> getRater();


    class Factory {
        public static RaterAPI service;

        public static RaterAPI getInstance() {

            if (service == null) {
                Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())
                        .baseUrl(baseUrl)
                        .build();
                service = retrofit.create(RaterAPI.class);
                return service;
            } else {
                return service;
            }
        }
    }
}
主页中的

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.btnStart)
    Button btnStart;
    @BindView(R.id.tvRate)
    TextView tvRate;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.btnStart)
    public void setBtnStart() {

        RaterAPI.Factory.getInstance().getRater().enqueue(new Callback<Rater>() {
            @Override
            public void onResponse(Call<Rater> call, Response<Rater> response) {
                tvRate.setText(Double.toString(response.body().getMid()));

            }

            @Override
            public void onFailure(Call<Rater> call, Throwable t) {
                Log.e("Failed", t.getMessage());
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

它解决了我的问题:

RaterAPI.Factory.getInstance().getRater().enqueue(new Callback<RootObject>() {
            @Override
            public void onResponse(Call<RootObject> call, Response<RootObject> response) {
                tvRate.setText(Double.toString(response.body().getRates().get(0).getMid()));
            }