【问题标题】:Parse data from openweathermap using retrofit2 (using latitude and longitude)使用retrofit2解析来自openweathermap的数据(使用纬度和经度)
【发布时间】:2021-08-08 08:01:19
【问题描述】:

我一直在尝试使用 openweathermap api 使用用户当前位置(即纬度和经度)来获取天气,我已经正确检索了坐标,但是当我运行应用程序时,我在 logcat 中收到错误 404。这是我的代码

private void setData(double latitude, double longitude) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.openweathermap.org/data/2.5/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        WeatherAPI api = retrofit.create(WeatherAPI.class);

        Integer latitudeInt = (int) latitude;
        Integer longitudeInt = (int) longitude;

        Log.v("LATITUDE STRING", String.valueOf(latitudeInt));
        Log.v("LONGITUDE STRING", String.valueOf(longitudeInt));

        Call<WeatherResponse> call = api.getCurrentWeatherData(String.valueOf(latitudeInt), String.valueOf(longitudeInt), appId);
        call.enqueue(new Callback<WeatherResponse>() {
            @Override
            public void onResponse(Call<WeatherResponse> call, Response<WeatherResponse> response) {

                Log.v("RESPONSE", String.valueOf(response.code()));

                if (response.code() == 200) {
                    WeatherResponse weatherResponse = response.body();

                    assert weatherResponse != null;
                    double temp = weatherResponse.main.getTemp() - 273.15;
                    mainWeather.setText(String.valueOf(temp));

                    SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
                    Date currentDate = new Date(System.currentTimeMillis());
                    String dateString = formatter.format(currentDate);
                    date.setText(dateString);

                    double wind = weatherResponse.wind.getSpeed() * 2.23;
                    windSpeed.setText(String.valueOf(wind));

                    pressure.setText(String.valueOf(weatherResponse.main.getPressure()));

                    humidity.setText(String.valueOf(weatherResponse.main.getHumidity()));

                    feelsLike.setText(String.valueOf(weatherResponse.main.getFeelsLike()));

                    setImage(weatherResponse);
                }
            }

            @Override
            public void onFailure(Call<WeatherResponse> call, Throwable t) {
                mainWeather.setText("Error!");
            }
        });
    }

这里是 WeatherApi 查询接口

public interface WeatherAPI {
    @GET("data/2.5/weather?")
    Call<WeatherResponse> getCurrentWeatherData(@Query("lat") String lat, @Query("lon") String lon, @Query("APPID") String app_id);
}

我已经从我的另一个天气应用程序中复制了数据类,该应用程序使用用户的城市输入来显示天气,它运行良好,但这个应用程序根本没有填充视图。在 logcat 中出现错误 404。

【问题讨论】:

  • 我刚刚得到了解决方案,而不是 @GET("data/2.5/weather?") 我不得不使用 @GET("weather") 但为什么它仍然工作未知
  • 其他 App 可能设置了不同的 baseUrl。

标签: java android retrofit2 openweathermap


【解决方案1】:

您好,404 表示网页/网址不存在,在您发布的第一个代码中,您一直在尝试访问错误的 API,这就是您收到 404 https://en.wikipedia.org/wiki/HTTP_404 的原因

正确的 api - https://api.openweathermap.org/data/2.5/weather?

你试图访问什么-https://api.openweathermap.org/data/2.5/data/2.5/weather?

在改造 baseurl 中应该只有基本示例 - https://api.openweathermap.org/

应该在接口api调用中提供端点@GET("data/2.5/weather?")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-11
    • 2017-12-05
    • 2010-11-21
    • 2013-03-01
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    相关资源
    最近更新 更多