
應用程序開發中不可或缺的開放API
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class NewsCatcherAPICall {
public static void main(String[] args) {
String apiUrl = "http://m.dlbhg.com/api/scd2024053015961a1c65e5";
try {
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine())!= null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
} else {
System.out.println("Error: " + responseCode);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
void curlCallback(char *data, size_t size, size_t nmemb, void *userp) {
size_t realsize = size * nmemb;
char *buffer = (char *)userp;
memcpy(buffer + strlen(buffer), data, realsize);
}
int main() {
CURL *curl;
CURLcode res;
char apiUrl[] = "http://m.dlbhg.com/api/scd2024053015961a1c65e5";
char response[10240] = {0};
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, apiUrl);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);
res = curl_easy_perform(curl);
if(res!= CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
} else {
printf("%s\n", response);
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
ruby
<h3>Ruby集成API案例</h3>
require 'net/http'
require 'json'
url = URI("http://m.dlbhg.com/api/scd2024053015961a1c65e5")
request = Net::HTTP::Post.new(url)
request.content_type = 'application/json'
request.body = { query: 'example search' }.to_json
response = Net::HTTP.start(url.host, url.port, use_ssl: true) do |http|
http.request(request)
end
puts JSON.parse(response.body)
如果需要替換方案,可以考慮其他新聞聚合服務或使用開源工具自行開發。下面提供一些可替換方案,點擊鏈接直接訪問:
要找到世界實時新聞聚合API,可以訪問冪簡集成API平臺,通過關鍵詞搜索或API分類頁瀏覽。冪簡集成平臺提供了多種API集成解決方案,幫助開發者快速找到并集成所需服務。
世界實時新聞聚合API是一個功能強大、安全可靠的工具,適用于多種應用場景。無論是企業還是個人開發者,都可以利用這項API獲取實時新聞數據,以支持決策制定、內容創作和市場分析。