编程语言
首页 > 编程语言> > java.lang中. RuntimeException找不到Retrofit批注. (参数3)

java.lang中. RuntimeException找不到Retrofit批注. (参数3)

作者:互联网

我正在尝试更新此RetroFit + Otto tutorial,所以我更新的代码是:

IWeather.java

RetroFit 2不允许返回空值,因此我将Call< Weather>添加为空getWeather(…),而不是空值.的GetWeather(…).

public interface IWeather {

    @GET("/{latitude},{longitude}")
    Call<Weather> getWeather(@Path("latitude") String latitude,
                             @Path("longitude") String longitude,
                             Callback<Weather> callback);
} 

ForecastClient.java

RetroFit 2.更改了其构造函数,因此新的ForecastClient具有下一种形式. IllegalArgumentException指向weather.getWeather(…)方法.

public class ForecastClient {

    private static final String BASE_URL = "https://api.darksky.net/forecast/";
    private static final String API_KEY = "******************";
    public static final String API_URL = BASE_URL + API_KEY + "/";

    private static ForecastClient mForecastClient;
    private static Retrofit mRetroAdapter;

    public static ForecastClient getClient() {
        if (mForecastClient == null) {
            mForecastClient = new ForecastClient();
        }
        return mForecastClient;
    }

    private ForecastClient() {
        mRetroAdapter = new Retrofit.Builder()
                .baseUrl(API_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(new OkHttpClient())
                .build();
    }

    public void getWeather(String latitude, String longitude, Callback<Weather> callback) {
        IWeather weather = mRetroAdapter.create(IWeather.class);
        weather.getWeather(latitude, longitude, callback);
    }
}

ForecastManager.java

public class ForecastManager {

    private Context mContext;
    private Bus mBus;
    private ForecastClient sForecastClient;

    public ForecastManager(Context context, Bus bus) {
        this.mContext = context;
        this.mBus = bus;
        sForecastClient = ForecastClient.getClient();
    }

    @Subscribe
    public void onGetWeatherEvent(GetWeatherEvent getWeatherEvent) {
        String latitude = Double.toString(getWeatherEvent.getLatitude()).trim();
        String longitude = Double.toString(getWeatherEvent.getLongitude()).trim();

        Callback<Weather> callback = new Callback<Weather>() {
            @Override
            public void onResponse(Call<Weather> call, Response<Weather> response) {
                Log.d(ForecastManager.class.getSimpleName(), response.body().toString());
                mBus.post(new SendWeatherEvent(response.body()));
            }

            @Override
            public void onFailure(Call<Weather> call, Throwable t) {
                Log.e(ForecastManager.class.getSimpleName(), t.getMessage());
            }
        };

        sForecastClient.getWeather(latitude, longitude, callback);
    }
}

我收到未找到No Retrofit批注,并且原因可能与回调有关,但它是RetroFit回调,因此我不明白为什么会出错.

堆栈跟踪指向MainActivity,特别是指向具有Java.lang.RuntimeException的Otto方法post():无法分派事件,原因是先前提到的java.lang.IllegalArgumentException的错误:未找到Retrofit批注. (参数3):

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.activity_main_textView)
    TextView textView;
    @BindView(R.id.activity_main_button)
    Button button;

    private static final double LATITUDE = **.******;
    private static final double LONGITUDE = **.******;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);
    }

    @OnClick(R.id.activity_main_button)
    public void onClick(View view) {
        BusProvider.getInstace().post(new GetWeatherEvent(LATITUDE, LONGITUDE));
    }

    @Subscribe
    public void onSendWeatherEvent(SendWeatherEvent sendWeatherEvent) {
        Weather weather = sendWeatherEvent.getWeather();
        Currently currently = weather.getCurrently();
        textView.setText(currently.getSummary());
    }

关于错误在哪里的任何线索,或者我应该如何处理总线订阅,回调等?

解决方法:

java.lang.IllegalArgumentException:未找到Retrofit注释. (参数3)

如错误所述,问题是getWeather方法的第三个参数没有注释. Callback类用于Call#enqueue(Callback)方法.

只需将sForecastClient.getWeather(纬度,经度,回调)更改为sForecastClient.getWeather(纬度,经度).排队(回调)并删除第三个参数.

标签:otto,android,java,retrofit
来源: https://codeday.me/bug/20191011/1895634.html