Wie man historische Daten aus Umgebungsweather ziehtJava

Java-Forum
Anonymous
 Wie man historische Daten aus Umgebungsweather zieht

Post by Anonymous »

Ich versuche zu verstehen, wie die API für Umgebungswetter verwendet werden kann (https://ambientweather.docs.apiary.io) und ich dachte, ich wüsste, was ich tun soll, aber mir fehlt etwas.

Code: Select all

public static void main(String[] args) throws Exception {
String API_URL = "https://api.ambientweather.net/v1/devices";
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -7);
long endDate = calendar.getTimeInMillis();
System.out.println("attempting to pull data using " + endDate + " which is " + new Date(endDate));
int limit = 2;

String url = String.format("%s?applicationKey=%s&apiKey=%s&endDate=%s&limit=%s", API_URL, applicationKey, apiKey, endDate, limit);
System.out.println("URL [" + url + "]");

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet request = new HttpGet(url);
org.apache.hc.client5.http.impl.classic.CloseableHttpResponse response = httpClient.execute(request);

HttpEntity entity = response.getEntity();
String jsonResponse = EntityUtils.toString(entity);
System.out.println("output [" + jsonResponse + "]");

int dateUtcLoc = jsonResponse.indexOf("\"dateutc\":");
while (dateUtcLoc > -1) {
int commaAfterLoc = jsonResponse.indexOf(",",dateUtcLoc);
String datapointMsString = jsonResponse.substring(dateUtcLoc + 10, commaAfterLoc);
System.out.println("received " + datapointMsString + " which is " + new Date(Long.parseLong(datapointMsString)));
jsonResponse = jsonResponse.substring(commaAfterLoc);
dateUtcLoc = jsonResponse.indexOf("\"dateutc\":");
}
response.close();
}
< /code>
Die Ausgabe, die ich erhalte, ist nur ein Datenpunkt von "Now".  Ich hätte zwei Datenpunkte und vor einer Woche erwartet. < /P>
attempting to pull data using 1740525231967 which is Tue Feb 25 18:13:51 EST 2025
URL [https://api.ambientweather.net/v1/devices?applicationKey= ... &apiKey= ... &endDate=1740525231967&limit=2]
output [[{"macAddress":"30:83 ... ","lastData":{"dateutc":1741129920000,"tempf":64.2,"humidity":60,"windspeedmph":2.01,"windgustmph":2.24,"maxdailygust":15.88,"winddir":207,"uv":0,"solarradiation":2.02,"hourlyrainin":0,"dailyrainin":0,"weeklyrainin":0,"monthlyrainin":0,"totalrainin":112.378,"battout":1,"tempinf":70.3,"humidityin":44,"baromrelin":29.905,"baromabsin":29.291,"temp1f":57.9,"humidity1":66,"temp2f":55.8,"humidity2":63,"batt1":1,"batt2":1,"batt_co2":1,"feelsLike":64.2,"dewPoint":50.02,"feelsLike1":57.9,"dewPoint1":46.6,"feelsLike2":55.8,"dewPoint2":43.4,"feelsLikein":69.1,"dewPointin":47.3,"lastRain":"2025-02-27T21:59:00.000Z","tz":"America/New_York","date":"2025-03-04T23:12:00.000Z"},"info": ...
received 1741129920000 which is Tue Mar 04 18:12:00 EST 2025

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post