javaandroidgenericsinterfacegeneric-interface

Unchecked call to method as a member of raw type


Following warning is shown in my project -

Unchecked call to 'getWeatherData(T,Boolean,String)' as a member of raw type 'IWeatherCallbackListener'.

I have created following interface -

public interface IWeatherCallbackListener<T> {
 void getWeatherData(T weatherModel, Boolean success, String errorMsg);
}

And called it in following way,

public class WeatherConditions {

private static IWeatherApi mWeatherApi;

/**
 * @param city
 * @param appId
 * @param listener
 */
public static void getOpenWeatherData(String city, String appId, IWeatherCallbackListener listener) {

    mWeatherApi = ApiService.getRetrofitInstance(BASE_URL_OPEN_WEATHER).create(IWeatherApi.class);
    Call<OpenWeatherModel> resForgotPasswordCall = mWeatherApi.getOpenWeatherData(appId, city);
    resForgotPasswordCall.enqueue(new Callback<OpenWeatherModel>() {
        @Override
        public void onResponse(Call<OpenWeatherModel> call, Response<OpenWeatherModel> response) {
            if (response.body() != null) {
                if (listener != null)
                    listener.getWeatherData(response.body(), true, "");
            }
        }

        @Override
        public void onFailure(Call<OpenWeatherModel> call, Throwable t) {
            if (listener != null)
                 listener.getWeatherData(null, false, t.getMessage());
        }
    });
}

I have implemented this interface in my MainActivity and called the method as -

WeatherConditions.getOpenWeatherData(etCityName.getText().toString(), OPEN_WEATHER_APP_ID, MainActivity.this)

Can anyone please help and explain this warning.


Solution

  • Looks like you have to declare your T type too, in your case it has to be a class of response.body() instance.

    Try to replace line

    public static void getOpenWeatherData(String city, String appId, IWeatherCallbackListener listener)
    

    to

    public static void getOpenWeatherData(String city, String appId, IWeatherCallbackListener<ResponseBody> listener)
    

    That is so because when you declare your interface

    IWeatherCallbackListener<T>
    

    You use T and its raw type. When you create instance you have to show what exactly type you will use or what exactly type you want to receive as argument.

    For example if you create that listener manually that had to look like this

    IWeatherCallbackListener<ResponseBody> listener = new IWeatherCallbackListener<ResponseBody>() {
        //implementation of methods
    }
    

    Same thing for arguments, you have to show what T you can receive.