Search in sources :

Example 1 with Ticker

use of it.angelic.mpw.model.jsonpojos.coinmarketcap.Ticker in project MPW by shineangelic.

the class Utils method synchCurrenciesFromCoinmarketcap.

public static void synchCurrenciesFromCoinmarketcap(Context ctx, CurrencyEnum mCur) {
    try {
        // synch jason
        RequestFuture<JSONArray> future = RequestFuture.newFuture();
        JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, Constants.ETHER_STATS_URL, new JSONArray(), future, future);
        JSONClientSingleton.getInstance(ctx).addToRequestQueue(request);
        // this will block
        JSONArray response = future.get();
        Log.d(Constants.TAG, response.toString());
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();
        Type listType = new TypeToken<List<Ticker>>() {
        }.getType();
        List<Ticker> posts = gson.fromJson(response.toString(), listType);
        Ticker fnd = null;
        for (Ticker currency : posts) {
            if (mCur.name().equalsIgnoreCase(currency.getSymbol()) || mCur.toString().equalsIgnoreCase(currency.getName())) {
                fnd = currency;
            }
            // always save ETH
            if (CurrencyEnum.ETH.name().equalsIgnoreCase(currency.getSymbol())) {
                CryptoSharedPreferencesUtils.saveEthereumValues(currency, ctx);
            }
            // always save BTC
            if (CurrencyEnum.BTC.name().equalsIgnoreCase(currency.getSymbol())) {
                CryptoSharedPreferencesUtils.saveBtcValues(currency, ctx);
            }
        }
        CryptoSharedPreferencesUtils.saveEtherValues(fnd, ctx);
    } catch (Exception e) {
        Log.d(Constants.TAG, "ERROR DURING COINMARKETCAP: " + e.getMessage());
    }
}
Also used : JsonArrayRequest(com.android.volley.toolbox.JsonArrayRequest) Type(java.lang.reflect.Type) GsonBuilder(com.google.gson.GsonBuilder) Ticker(it.angelic.mpw.model.jsonpojos.coinmarketcap.Ticker) JSONArray(org.json.JSONArray) Gson(com.google.gson.Gson) List(java.util.List) IllegalFormatException(java.util.IllegalFormatException)

Example 2 with Ticker

use of it.angelic.mpw.model.jsonpojos.coinmarketcap.Ticker in project MPW by shineangelic.

the class MPWCoinmarketcapService method saveCurrencyValue.

public static void saveCurrencyValue(JSONArray response, CurrencyEnum mCur, Context ctx) {
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();
    Type listType = new TypeToken<List<Ticker>>() {
    }.getType();
    List<Ticker> posts = gson.fromJson(response.toString(), listType);
    Ticker fnd = null;
    for (Ticker currency : posts) {
        if (mCur.name().equalsIgnoreCase(currency.getSymbol()) || mCur.toString().equalsIgnoreCase(currency.getName())) {
            fnd = currency;
        }
        // always save ETH
        if (CurrencyEnum.ETH.name().equalsIgnoreCase(currency.getSymbol())) {
            CryptoSharedPreferencesUtils.saveEthereumValues(currency, ctx);
        }
        // always save BTC
        if (CurrencyEnum.BTC.name().equalsIgnoreCase(currency.getSymbol())) {
            CryptoSharedPreferencesUtils.saveBtcValues(currency, ctx);
        }
    }
    // eventually resets  when fnd = null
    CryptoSharedPreferencesUtils.saveEtherValues(fnd, ctx);
}
Also used : Type(java.lang.reflect.Type) GsonBuilder(com.google.gson.GsonBuilder) Ticker(it.angelic.mpw.model.jsonpojos.coinmarketcap.Ticker) Gson(com.google.gson.Gson) List(java.util.List)

Aggregations

Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 Ticker (it.angelic.mpw.model.jsonpojos.coinmarketcap.Ticker)2 Type (java.lang.reflect.Type)2 List (java.util.List)2 JsonArrayRequest (com.android.volley.toolbox.JsonArrayRequest)1 IllegalFormatException (java.util.IllegalFormatException)1 JSONArray (org.json.JSONArray)1