Retrofit2无法从API检索每月货币数据

时间:2019-06-06 20:31:12

标签: java android retrofit

我正在尝试制作一个应用,该应用将显示由他指定的用户每月货币汇率。现在,仅出于测试目的,我想获得每月的美元汇率。但是我已经尝试过几种方法来做到这一点,并且每次我的应用崩溃时。这是“最新”的一种尝试。

改造电话

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

    textView = (TextView) findViewById(R.id.error_test);

    String url = "https://api.exchangeratesapi.io/";
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    Api api = retrofit.create(Api.class);
    Call<testModel> modelCall = api.getMonthTest("2018-01-01", "2018-02-01", "USD");
    modelCall.enqueue(new Callback<testModel>() {
        @Override
        public void onResponse(Call<testModel> call, Response<testModel> response) {
            if (!response.isSuccessful()) {
                textView.setText(response.code());
            }

            List<testModel> models = Collections.singletonList(response.body());
            for (testModel model : models) {
                model.rates.put("2018-01-19",model.testRATES.getUsd());
                Iterator<Map.Entry<String, Double>> iterator = model.getRates().entrySet().iterator();
                while (iterator.hasNext()) {
                    Map.Entry entry = (Map.Entry) iterator.next();

                    key = (String) entry.getKey();
                    value = (Double) entry.getValue();

                    textView.append(key+"    "+value+"\n");
                }
            }
        }
        @Override
        public void onFailure(Call<testModel> call, Throwable t) {
            textView.setText(t.getMessage());
            System.out.println(t.getMessage());
        }
    });
 }
}

接口方法

@GET("history")
Call<testModel> getMonthTest(
        @Query("start_at") String startAt,
        @Query("end_at") String endAt,
        @Query("symbols") String symbol);

POJO类

@SerializedName("end_at")
String endAt;
@SerializedName("start_at")
String startAt;
@SerializedName("rates")
Map<String, Double> rates;

Rates testRATES;


public String getEndAt() {
    return endAt;
}

public String getStartAt() {
    return startAt;
}

public Map<String, Double> getRates() {
    return rates;
}

public class Rates{
    @SerializedName("USD")
    Double usd;

    public Double getUsd() {
        return usd;
    }
}

JSON响应

  {
  "base": "EUR",
  "rates": {
    "2018-01-30": {
      "USD": 1.2421
    },
    "2018-01-15": {
      "USD": 1.2277
    },
    "2018-01-03": {
      "USD": 1.2023
    },
    "2018-02-01": {
      "USD": 1.2459
    },
    "2018-01-26": {
      "USD": 1.2436
    },
    "2018-01-12": {
      "USD": 1.2137
    },
    "2018-01-19": {
      "USD": 1.2255
    },
    "2018-01-16": {
      "USD": 1.223
    },
    "2018-01-22": {
      "USD": 1.2239
    },
    "2018-01-10": {
      "USD": 1.1992
    },
    "2018-01-08": {
      "USD": 1.1973
    },
    "2018-01-25": {
      "USD": 1.2407
    },
    "2018-01-29": {
      "USD": 1.2379
    },
    "2018-01-24": {
      "USD": 1.2352
    },
    "2018-01-11": {
      "USD": 1.2017
    },
    "2018-01-02": {
      "USD": 1.2065
    },
    "2018-01-31": {
      "USD": 1.2457
    },
    "2018-01-05": {
      "USD": 1.2045
    },
    "2018-01-18": {
      "USD": 1.2235
    },
    "2018-01-23": {
      "USD": 1.2249
    },
    "2018-01-04": {
      "USD": 1.2065
    },
    "2018-01-17": {
      "USD": 1.2203
    },
    "2018-01-09": {
      "USD": 1.1932
    }
  },
  "end_at": "2018-02-01",
  "start_at": "2018-01-01"
}

错误

java.lang.IllegalStateException: Expected a double but was BEGIN_OBJECT at line 1 column 38 path $.rates.

哪里有问题?

2 个答案:

答案 0 :(得分:0)

您的模型是错误的。 ratesMap<String, Map<String, Double>>

答案 1 :(得分:0)

问题出在@SerializedName("rates") Map<String, Double> rates;

您有:

 "rates": {
    "2018-01-30": {
      "USD": 1.2421
    }
 }

因此,您在Map<String, Double>中应该有Map<String, Rate>,如下所示:

public class Rate {
    @SerializedName("USD")
    Double usd;

    public Double getUsd() {
        return usd;
    }
}

不确定Map<String, Rate>是否可以工作,因为字符串不是键,而是动态的,因此您可能必须进行一些自定义反序列化。

参考: