应用无法从改装API获取所有数据

时间:2019-05-02 21:41:55

标签: java android retrofit retrofit2

我完成了https://www.simplifiedcoding.net/retrofit-android-example/的花絮 我想创建自己的调用,想从API最新的货币兑换处获取,但是我的模型类遇到了一个问题(我想就是这样)。我更改了一些代码,并接收了“基准”和“日期”数据,但是当我试图获取一些货币值(例如,以BGN为例)时,程序始终显示0.0。我该如何解决?我对那个模型课正确吗?有问题吗?

型号

public class Model  {
private String base,date;
private double BGN;

public Model(String base, String date, double BGN) {
    this.base = base;
    this.date = date;
    this.BGN = BGN;
}

public String getBase() {
    return base;
}

public String getDate() {
    return date;
}
public double getBGN() {
    return BGN;
 }
}


Api (interface)

public interface Api {

String BASE_URL = "https://api.exchangeratesapi.io/";

@GET("latest")
Call<Model> getCurrency();
}

MainActivity

public class MainActivity extends AppCompatActivity {

private TextView textViewResult;

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

    textViewResult = (TextView) findViewById(R.id.label);

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Api.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    Api api = retrofit.create(Api.class);
    Call<Model> call = api.getCurrency();
    call.enqueue(new Callback<Model>() {
        @Override
        public void onResponse(Call<Model> call, Response<Model> response) {

            List<Model> currencyList = Collections.singletonList(response.body());

            for (Model currency : currencyList) {
                String content = "";
                content += "Base: " + currency.getBase() + "\n";
                content += "Date: " + currency.getDate() + "\n";
                content += "BGN: " + currency.getBGN() + "\n";
                textViewResult.append(content);
            }
        }
        @Override
        public void onFailure(Call<Model> call, Throwable t) {
            Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();

        }
    });
 }
}

1 个答案:

答案 0 :(得分:1)

要从改造中获取所有数据,您需要两个POJO类。他们需要像这样,在其中为所需的所有费率添加方法和变量:

public class Model {
 private String base;
 private String date;
 Rates RatesObject;


 // Getter Methods 

 public String getBase() {
  return base;
 }

 public String getDate() {
  return date;
 }

 public Rates getRates() {
  return RatesObject;
 }

 // Setter Methods 

 public void setBase(String base) {
  this.base = base;
 }

 public void setDate(String date) {
  this.date = date;
 }

 public void setRates(Rates ratesObject) {
  this.RatesObject = ratesObject;
 }
}


public class Rates {
 private float CAD;
 private float CHF;
 private float GBP;
 private float SEK;
 private float EUR;
 private float USD;


 // Getter Methods 

 public float getCAD() {
  return CAD;
 }

 public float getCHF() {
  return CHF;
 }

 public float getGBP() {
  return GBP;
 }

 public float getSEK() {
  return SEK;
 }

 public float getEUR() {
  return EUR;
 }

 public float getUSD() {
  return USD;
 }

 // Setter Methods 

 public void setCAD(float CAD) {
  this.CAD = CAD;
 }

 public void setCHF(float CHF) {
  this.CHF = CHF;
 }

 public void setGBP(float GBP) {
  this.GBP = GBP;
 }

 public void setSEK(float SEK) {
  this.SEK = SEK;
 }

 public void setEUR(float EUR) {
  this.EUR = EUR;
 }

 public void setUSD(float USD) {
  this.USD = USD;
 }
}