![]() |
![]() |
| Paypal | FTP | CCD | Buscar | Trucos | Trabajo | Foros |
|
|||||||
| Registrarse | FAQ | Miembros | Calendario | Guía de estilo | Temas de Hoy |
![]() |
|
|
Herramientas | Buscar en Tema | Desplegado |
|
#1
|
|||
|
|||
|
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
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;
}
}
//---------------------------------------------------------------------------
![]() Última edición por navbuoy fecha: 11-06-2025 a las 16:39:28. |
|
#2
|
||||
|
||||
![]() ![]() ![]()
__________________
La otra guía de estilo | Búsquedas avanzadas | Etiquetas para código | Colabora mediante Paypal |
|
#3
|
||||
|
||||
![]() ![]() 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 :
__________________
Germán Estévez => Web/Blog Guía de estilo, Guía alternativa Utiliza TAG's en tus mensajes. Contactar con el Clubdelphi ![]() P.D: Más tiempo dedicado a la pregunta=Mejores respuestas. |
![]() |
|
|
Temas Similares
|
||||
| Tema | Autor | Foro | Respuestas | Último mensaje |
| Buenas Noticias | faustoffp | Noticias | 0 | 04-09-2006 06:33:06 |
|