Aqui va otro ejemplo sobre como conectar con esta web (GNews.io) para obtener noticias online
podeis registraros como cuenta FREE para hacer 100 consultas a la API cada 24 horas
utilizamos un TNetHttpClient, un TMemo y un TButton
Unit1.h
Código:
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <System.Net.HttpClient.hpp>
#include <System.Net.HttpClientComponent.hpp>
#include <System.Net.URLClient.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TMemo *Memo1;
TButton *Button1;
TNetHTTPClient *NetHTTPClient1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Unit1.cpp
Código:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <System.JSON.hpp>
#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)
{
String apiKey = "TU API KEY"; // Reemplazá con tu clave
String url = "https://gnews.io/api/v4/top-headlines?lang=es&country=sp&token=" + apiKey;
TMemoryStream *responseStream = new TMemoryStream();
try
{
// Realiza la solicitud GET
NetHTTPClient1->Get(url, responseStream);
responseStream->Position = 0;
// Leer el stream como bytes y convertir con UTF8
TBytes bytes;
bytes.Length = responseStream->Size;
responseStream->ReadBuffer(bytes, responseStream->Size);
String json = TEncoding::UTF8->GetString(bytes);
TJSONObject *root = (TJSONObject*)TJSONObject::ParseJSONValue(json);
TJSONArray *articles = (TJSONArray*)root->GetValue("articles");
for (int i = 0; i < articles->Count; i++)
{
TJSONObject *noticia = (TJSONObject*)articles->Items[i];
String titulo = noticia->GetValue("title")->Value();
String link = noticia->GetValue("description")->Value();
String description = noticia->GetValue("content")->Value();
String content = noticia->GetValue("url")->Value();
Memo1->Lines->Add(titulo);
Memo1->Lines->Add(link);
Memo1->Lines->Add(description);
Memo1->Lines->Add(content);
Memo1->Lines->Add("------------------------");
}
delete root;
}
catch (const Exception &e)
{
Memo1->Lines->Text = "Error: " + e.Message;
}
}
//---------------------------------------------------------------------------
y aqui podeis ver el resultado:
