我尝试从Currency api响应中获取,该响应应为我显示用户货币指定的每月值,但是我在进行正确的改造调用时遇到了问题。哪里可能有问题?
我遇到的错误
java.lang.IllegalStateException:预期为字符串,但在第1行第38列$ .rates处为BEGIN_OBJECT。
带有改造调用的方法
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){
String content = "";
content += model.testRATES.getUsd();
textView.append(content);
}
}
@Override
public void onFailure(Call<testModel> call, Throwable t) {
textView.setText(t.getMessage());
}
});
}
}
与api方法调用的接口
@GET("history")
Call<testModel> getMonthTest(
@Query("start_at") String startAt,
@Query("end_at") String endAt,
@Query("symbols") String symbol);
POJO clas
public class testModel {
String base;
@SerializedName("end_at")
String endAt;
@SerializedName("start_at")
String startAt;
Map<String, String> rates;
Rates testRATES;
public String getBase() {
return base;
}
public String getEndAt() {
return endAt;
}
public String getStartAt() {
return startAt;
}
public Map<String, String> 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"
}
编辑
POJO类
@SerializedName("rates")
Map<String, Rates> rates;
改造电话
List<testModel> models = Collections.singletonList(response.body());
for (testModel model : models) {
currencyMap.put("2018-01-19",model.testRATES.getUsd());
Iterator<Map.Entry<String, Double>> iterator = currencyMap.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");
}
}
现在,我收到此错误
Attempt to invoke virtual method 'java.lang.Double com.example.currencyexchange.testModel$Rates.getUsd()' on a null object reference
答案 0 :(得分:0)
在您的testModel
(应该为TestModel
)中,您拥有:
Map<String, String> rates;
在JSON中,有一个包含一些日期和Rates
(应为Rate
)的地图。日期为键,Rates
为值。如果您想将日期反序列化为字符串,请在上方更改为:
Map<String, Rates> rates;
答案 1 :(得分:-1)
代替
Map<String, String> rates;
你能尝试
@SerializedName("rates")
List<Rates>rates