Club Delphi  
    Paypal   FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Otros entornos y lenguajes > C++ Builder
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 11-06-2025
navbuoy navbuoy is offline
Miembro
 
Registrado: mar 2024
Posts: 360
Poder: 3
navbuoy Va por buen camino
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:


Última edición por navbuoy fecha: 11-06-2025 a las 16:39:28.
Responder Con Cita
  #2  
Antiguo 11-06-2025
Avatar de Casimiro Noteví
Casimiro Noteví Casimiro Noteví is offline
Merodeador
 
Registrado: sep 2004
Ubicación: En algún lugar.
Posts: 32.669
Poder: 10
Casimiro Noteví Tiene un aura espectacularCasimiro Noteví Tiene un aura espectacular
Responder Con Cita
  #3  
Antiguo 12-06-2025
Avatar de Neftali [Germán.Estévez]
Neftali [Germán.Estévez] Neftali [Germán.Estévez] is offline
[becario]
 
Registrado: jul 2004
Ubicación: Barcelona - España
Posts: 19.435
Poder: 10
Neftali [Germán.Estévez] Es un diamante en brutoNeftali [Germán.Estévez] Es un diamante en brutoNeftali [Germán.Estévez] Es un diamante en bruto

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
__________________
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.
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Buenas Noticias faustoffp Noticias 0 04-09-2006 06:33:06


La franja horaria es GMT +2. Ahora son las 03:28:18.


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
Copyright 1996-2007 Club Delphi