OpenWeather API 以意外结果响应

OpenWeather API responds with unexpected results

出于某种原因,Api 响应设置了与我发送的坐标不同的坐标。我调试了应用程序,调用 api 的结果与 JSON 文件中返回的结果不同。例如 发送者:

  1. http://api.openweathermap.org/data/2.5/weather?q=lat=51.89689166666667&lon=-8.486315

返回者

  1. {"coord":{"lon":24.76,"lat":60.15},"weather":...... ………… 这是我目前使用的代码:

    私有静态最终字符串OPEN_WEATHER_MAP_API = “http://api.openweathermap.org/data/2.5/weather?q=”;

       URL url = new URL(String.format(OPEN_WEATHER_MAP_API + "lat="+lat+"&"+"lon="+lon));
            HttpURLConnection connection =
                    (HttpURLConnection)url.openConnection();
    
            connection.addRequestProperty("x-api-key",
                    context.getString(R.string.open_weather_maps_app_id));
    
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(connection.getInputStream()));
    
            StringBuffer json = new StringBuffer(1024);
            String tmp="";
            while((tmp=reader.readLine())!=null)
                json.append(tmp).append("\n");
            reader.close();
    
            JSONObject data = new JSONObject(json.toString());
    

对 android 或这项服务一无所知,但我只是查看了他们的 API,因为我认为您的 URL 看起来很可疑,并发现了这个:

api.openweathermap.org/data/2.5/weather?lat=35&lon=139

请注意,您的示例和变量 private static final String OPEN_WEATHER_MAP_API = "http://api.openweathermap.org/data/2.5/weather?q=";

中没有 ?q=

当我删除 q=(并从他们的网站示例中添加演示 API 密钥)时,我们得到一个有效的 URL returns 指定数据 lat/long:
http://api.openweathermap.org/data/2.5/weather?lat=51.89689166666667&lon=-8.486315&appid=44db6a862fba0b067b1930da0d769e98