【问题标题】:OpenWeather API responds with unexpected resultsOpenWeather API 响应意外结果
【发布时间】:2016-02-16 21:02:48
【问题描述】:

由于某种原因,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 android-fragments location


    【解决方案1】:

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

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

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

    当我删除 q=(并从他们的网站示例中添加演示 API 密钥)时,我们会得到一个工作 URL返回指定纬度/经度的数据
    @987654322 @

    【讨论】:

      猜你喜欢
      • 2018-10-30
      • 2016-06-14
      • 2019-11-12
      • 1970-01-01
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      • 2021-11-28
      相关资源
      最近更新 更多