Lo he dividido en 2 List Box para que la URL sea mas facilmente accesible cuando usemos
el reproductor para tocar la emisora en la tarjeta de sonido
Código:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <System.JSON.hpp>
#include <IdHTTP.hpp>
#include <IdSSLOpenSSL.hpp>
#include <memory>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
try
{
std::unique_ptr<TIdHTTP> http(new TIdHTTP(nullptr));
std::unique_ptr<TIdSSLIOHandlerSocketOpenSSL> ssl(new TIdSSLIOHandlerSocketOpenSSL(nullptr));
std::unique_ptr<TStringStream> response(new TStringStream());
// Configurar SSL correctamente
ssl->SSLOptions->Method = sslvTLSv1_2;
ssl->SSLOptions->Mode = sslmClient;
ssl->SSLOptions->VerifyMode = TIdSSLVerifyModeSet();
ssl->SSLOptions->VerifyDepth = 0;
http->IOHandler = ssl.get(); // Asignar SSL a HTTP
http->HandleRedirects = true;
http->Request->UserAgent = "Mozilla/5.0";
// URL de la API de Radio Browser
String url = "http://de2.api.radio-browser.info/json/stations";
// Hacer la solicitud GET
http->Get(url, response.get());
// Parsear la respuesta JSON
std::unique_ptr<TJSONArray> jsonResponse(static_cast<TJSONArray*>(TJSONObject::ParseJSONValue(response->DataString)));
if (jsonResponse)
{
ListBox1->Items->Clear();
ListBox2->Items->Clear();
for (int i = 0; i < jsonResponse->Count; i++)
{
TJSONObject *station = dynamic_cast<TJSONObject*>(jsonResponse->Items[i]);
if (station)
{
TJSONString *jsonName = dynamic_cast<TJSONString*>(station->GetValue("name"));
TJSONString *jsonUrl = dynamic_cast<TJSONString*>(station->GetValue("url"));
UnicodeString name = jsonName ? jsonName->Value() : "Desconocido";
UnicodeString streamUrl = jsonUrl ? jsonUrl->Value() : "URL no disponible";
ListBox1->Items->Add(name);
ListBox2->Items->Add(streamUrl);
}
}
}
}
catch (const Exception &e)
{
ShowMessage("Error: " + e.Message);
}
}
//---------------------------------------------------------------------------