Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   C++ Builder (https://www.clubdelphi.com/foros/forumdisplay.php?f=13)
-   -   API de Noticias en GNews.io (https://www.clubdelphi.com/foros/showthread.php?t=97523)

navbuoy 11-06-2025 16:34:21

API de Noticias en GNews.io
 
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:


Casimiro Noteví 11-06-2025 16:47:48

^\||/^\||/^\||/

Neftali [Germán.Estévez] 12-06-2025 10:12:30

^\||/^\||/
Interesante.
Me estaba extrañando que no se pudiera filtrar por categorías, per ahora veo que si.

category general
This parameter allows you to change the category for the request.

The available categories are :
  • general
  • world
  • nation
  • business
  • technology
  • entertainment
  • sports
  • science
  • health


La franja horaria es GMT +2. Ahora son las 05:57:53.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi