ticker first approach

This commit is contained in:
Nicolás Sánchez 2024-12-12 10:05:39 -03:00
parent a0ca543492
commit a24b3ee260
2 changed files with 13 additions and 3 deletions

View File

@ -6,6 +6,7 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import java.io.IOException; import java.io.IOException;
import java.util.Locale;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request; import okhttp3.Request;
@ -26,12 +27,14 @@ public class TickerTracker {
public static PriceChangeData getPriceChanges(String symbol) throws IOException { public static PriceChangeData getPriceChanges(String symbol) throws IOException {
// Construct the API request URL for 24h change // Construct the API request URL for 24h change
Request request24h = new Request.Builder() Request request24h = new Request.Builder()
.url(BINANCE_API_BASE_URL + "?symbol=" + symbol) .url(BINANCE_API_BASE_URL + "?symbol=" + symbol.toUpperCase(Locale.ROOT))
.build(); .build();
// Send 24h request // Send 24h request
try (Response response24h = httpClient.newCall(request24h).execute()) { try (Response response24h = httpClient.newCall(request24h).execute()) {
// Parse the JSON response if (!response24h.isSuccessful()) {
throw new IOException("Unexpected code " + response24h);
}
String responseBody24h = response24h.body().string(); String responseBody24h = response24h.body().string();
JsonObject jsonResponse = JsonParser.parseString(responseBody24h).getAsJsonObject(); JsonObject jsonResponse = JsonParser.parseString(responseBody24h).getAsJsonObject();
@ -47,11 +50,14 @@ public class TickerTracker {
// Fetch historical data for 7d and 30d changes // Fetch historical data for 7d and 30d changes
Request historicalRequest = new Request.Builder() Request historicalRequest = new Request.Builder()
.url("https://api.binance.com/api/v3/klines?symbol=" + symbol + "&interval=1d&limit=30") .url("https://api.binance.com/api/v3/klines?symbol=" + symbol.toUpperCase(Locale.ROOT) + "&interval=1d&limit=30")
.build(); .build();
// Send historical data request // Send historical data request
try (Response historicalResponse = httpClient.newCall(historicalRequest).execute()) { try (Response historicalResponse = httpClient.newCall(historicalRequest).execute()) {
if (!historicalResponse.isSuccessful()) {
throw new IOException("Unexpected code " + historicalResponse);
}
String historicalResponseBody = historicalResponse.body().string(); String historicalResponseBody = historicalResponse.body().string();
// Calculate 7d and 30d percentage changes // Calculate 7d and 30d percentage changes

View File

@ -8,6 +8,7 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
@ -17,6 +18,8 @@ import com.example.dcav2gui.R;
import com.example.dcav2gui.TickerTracker; import com.example.dcav2gui.TickerTracker;
import com.example.dcav2gui.databinding.FragmentHomeBinding; import com.example.dcav2gui.databinding.FragmentHomeBinding;
import java.io.IOException;
public class HomeFragment extends Fragment { public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel; private HomeViewModel homeViewModel;
@ -220,6 +223,7 @@ public class HomeFragment extends Fragment {
String base_3 = "@string/base_price_ticker_3"; String base_3 = "@string/base_price_ticker_3";
String quote_3 = "@string/quote_price_ticker_3"; String quote_3 = "@string/quote_price_ticker_3";
TickerTracker.PriceChangeData priceData = TickerTracker.getPriceChanges( base_1 + quote_1); TickerTracker.PriceChangeData priceData = TickerTracker.getPriceChanges( base_1 + quote_1);
pricePair1.setText(String.format("%.2f", priceData.getCurrentPrice())); pricePair1.setText(String.format("%.2f", priceData.getCurrentPrice()));
pricePair124hPercentage.setText(String.format("%.2f%%", priceData.getPriceChangePercent24h())); pricePair124hPercentage.setText(String.format("%.2f%%", priceData.getPriceChangePercent24h()));