简体   繁体   中英

Parse data from openweathermap using retrofit2 (using latitude and longitude)

I've been trying to get weather using openweathermap api using the users current location (ie latitude and longitude), I have correctly retrieved the coordinated but when I run the app I get error 404 in logcat. Here is my code

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!");
            }
        });
    }

Here is the WeatherApi interface for querying

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

I've copied the data class from one of my other weather app that used user's city input to display weather and it worked fine, but this app isn't populating the views at all. and in logcat I get error 404.

hi there 404 means webpage/url not existing, in the first code you posted you have been trying to access a wrong API thats why you get 404 https://en.wikipedia.org/wiki/HTTP_404

correct api - https://api.openweathermap.org/data/2.5/weather ?

what you were trying to access- https://api.openweathermap.org/data/2.5/data/2.5/weather ?

in retrofit baseurl shuld only have the base example - https://api.openweathermap.org/

the endpoint should be provided in the interface api calls @GET("data/2.5/weather?")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM