0

I have problem with Retrofit.

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);
}

RESPONSE 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: fail retrof java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

1
  • There are no json arrays inside, just key value pairs. Commented Sep 21, 2018 at 22:46

2 Answers 2

2

Change Call<List<CryptoPrices>> to Call<Map<String,CryptoPrices>>. The response is not a list, it’s a map of strings (like ”BTC”) to data.

Sign up to request clarification or add additional context in comments.

Comments

0

Your API method:

@GET("/data/pricemulti")
Call<List<CryptoPrices>> multiCryptoPrices(@Query("fsyms") String crypto,
                               @Query("tsyms") String currency);

expects a list of CryptoPrices as an array [...] and you are providing an object {...}.

Good luck with your crypto-client!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.