我在翻新方面遇到问题。
CryptoCompareClient.java
public interface CryptoCompareClient {
@GET("/data/pricemulti")
Call<List<CryptoPrices>> multiCryptoPrices(@Query("fsyms") String crypto,
@Query("tsyms") String currency);
@GET("/data/price")
Call<CryptoPrices> singleCryptoPrice(@Query("fsym") String crypto,
@Query("tsyms") String currency);
}
响应JSON: (https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,ETH,LSK&tsyms=USD,EUR,PLN)
{“ BTC”:{“ USD”:6731.55,“ EUR”:5716.11,“ PLN”:24359.79},“ ETH”:{“ USD”:244.41,“ EUR”:207.73,“ PLN”:905.73 },“ LSK”:{“ USD”:3.42,“ EUR”:2.91,“ PLN”:12.49}}
RetrofitInterface.java
public class RetrofitInterface {
Retrofit retrofit = RetrofitClientInstance.getRetrofitInstance();
private static final String CURRENCY="USD,EUR,PLN,BTC";
public void multiCrypto(String names){
CryptoCompareClient client=retrofit.create(CryptoCompareClient.class);
Call<List<CryptoPrices>> call=client.multiCryptoPrices(names,CURRENCY);
call.enqueue(new Callback<List<CryptoPrices>>() {
@Override
public void onResponse(Call<List<CryptoPrices>> call, Response<List<CryptoPrices>> response) {
}
@Override
public void onFailure(Call<List<CryptoPrices>> call, Throwable t) {
Log.d("MyCrypto", "fail retrof "+t);
}
});
}
}
Logcat
D / MyCrypto:无法追溯java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在第1行第2列路径$ BEGIN_OBJECT中>
答案 0 :(得分:1)
您的API方法:
@GET("/data/pricemulti")
Call<List<CryptoPrices>> multiCryptoPrices(@Query("fsyms") String crypto,
@Query("tsyms") String currency);
期望CryptoPrices
的列表作为数组[...]
,并且您要提供对象{...}
。
祝您的加密客户端好运!
答案 1 :(得分:1)
将Call<List<CryptoPrices>>
更改为Call<Map<String,CryptoPrices>>
。响应不是列表,而是字符串(如”BTC”
)到数据的映射。