I have rest api request: api.coinmarketcap.com/v2/ticker/?convert=EUR As you can see, here one method "convert=EUR"(can't add one more). I make 4 request in one asyncTask and create pojo with gson (bad practice, i know):
public Currencies fetchItems(int capacity){
List<Currency> currencyList;
Uri.Builder uriBuilder = ENDPOINT.buildUpon().appendQueryParameter("limit","" + capacity);
String uriUsd = uriBuilder.build().toString();
String uriBtc = uriBuilder.appendQueryParameter("convert", "BTC").build().toString();
uriBuilder = ENDPOINT.buildUpon().appendQueryParameter("limit","" + capacity);
String uriRub = uriBuilder.appendQueryParameter("convert", "RUB").build().toString();
uriBuilder = ENDPOINT.buildUpon().appendQueryParameter("limit","" + capacity);
String uriEur = uriBuilder.appendQueryParameter("convert", "EUR").build().toString();
try {
String jsonString = getUrlString(uriUsd);
String jsonStringBtc = getUrlString(uriBtc);
String jsonStringRub = getUrlString(uriRub);
String jsonStringEur = getUrlString(uriEur);
Currencies currencies = deserializationCryptoRequest(jsonString);
Currencies btc = deserializationCryptoRequest(jsonStringBtc);
Currencies rub = deserializationCryptoRequest(jsonStringRub);
Currencies eur = deserializationCryptoRequest(jsonStringEur);
currencyList = new ArrayList<>(currencies.getData().values());
List<Currency> btcList = new ArrayList<>(btc.getData().values());
List<Currency> rubList = new ArrayList<>(rub.getData().values());
List<Currency> eurList = new ArrayList<>(eur.getData().values());
for(int i = 0; i < currencyList.size(); i++){
Currency item = currencyList.get(i);
Currency btcItem = btcList.get(i);
Currency rubItem = rubList.get(i);
Currency eurItem = eurList.get(i);
String bitmapPath = IconsManager.getIconsLinks().get(item.getSymbol());
item.setIconBitmap(downloadBitmap(bitmapPath));
item.addQoute("BTC", btcItem.getQuotes().get("BTC"));
item.addQoute("RUB", rubItem.getQuotes().get("RUB"));
item.addQoute("EUR", eurItem.getQuotes().get("EUR"));
}
return currencies;
} catch (IOException ioe){
Log.e(TAG, "Cannot get response from api: " + ioe);
}
return null;
}
I did this to store several conversions in memory. How can i do this with Retrofit? How to do more elegantly?
define your baseurl:
public class Constants {
public static final String BASE_URL = "https://api.coinmarketcap.com/v2/";
}
create retrofit client interface:
import io.reactivex.Single;
import retrofit2.http.GET;
import retrofit2.http.Path;
public interface CoinMarketClient {
@GET("listings")
Single<CoinMarketListingsReponse> getListings();
@GET("ticker/{id}")
Single<CoinMarketCurrencyResponse> getCurrency(@Path("id") int id);
@GET("ticker/{id}/?convert={fiat}")
Single<CoinMarketCurrencyResponse> getCurrency(@Path("id") int id,
@Path("fiat") String fiatCode);
}
create enum:
public enum FiatCode {
AUD,
BRL,
CAD,
CHF,
CLP,
CNY,
CZK,
DKK,
EUR,
GBP,
HKD,
HUF,
IDR,
ILS,
INR,
JPY,
KRW,
MXN,
MYR,
NOK,
NZD,
PHP,
PKR,
PLN,
RUB,
SEK,
SGD,
THB,
TRY,
TWD,
USD,
ZAR
}
initialize retrofit stuff:
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new
OkHttpClient.Builder().addInterceptor(interceptor).build();
Gson gson = new GsonBuilder()
.registerTypeAdapter(Quotes.class, new QuotesDeserializer())
.create();
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.client(client)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson));
Retrofit retrofit = builder.build();
coinMarketClient = retrofit.create(CoinMarketClient.class);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.