java.lang中。 RuntimeException未找到Retrofit注释。 (参数#3) [英] java.lang. RuntimeException No Retrofit annotation found. (parameter #3)
问题描述
我正在尝试更新此 RetroFit +奥托教程,所以我的代码更新为:
I'm trying to update this RetroFit + Otto tutorial, so my code updated is:
IWeather.java
RetroFit 2. +不允许返回 void
,所以代替 void getWeather(...)
我添加了呼叫<天气> getWeather(...)
。
RetroFit 2.+ doesn't allow to return void
, so instead of void getWeather(...)
I added Call<Weather> 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(...)
方法。
RetroFit 2.+ has changed his constructor, so the new ForecastClient
has the next form. The IllegalArgumentException
points to the weather.getWeather(...)
method.
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回调,所以我不明白为什么会出错。
I'm receiving a No Retrofit annotation found and I guess the reason is something related with my callback, but it is a RetroFit callback, so I don't understand why the error.
stacktrace指向MainActivity,in特别是Otto的方法 post()
带有 java.lang.RuntimeException:无法调度事件
,由先前提到的错误 java.lang.IllegalArgumentException:找不到Retrofit注释。 (参数#3)
:
The stacktrace points to the MainActivity, in particular to the Otto's method post()
with a java.lang.RuntimeException: Could not dispatch event
, caused by the error previously mentioned java.lang.IllegalArgumentException: No Retrofit annotation found. (parameter #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());
}
关于错误在哪里或如何处理总线订阅的任何线索,回调,等等?
Any clue about where is the error or how should I handle the bus subscriptions, the callback, etcetera?
推荐答案
java.lang.IllegalArgumentException:未找到Retrofit注释。 (参数#3)
如错误所示,问题是 getWeather的第三个参数
方法没有注释。 Callback
类用于 Call#enqueue(回调)
方法。
As the error says, the problem is that the third parameter of the getWeather
method does not have an annotation. The Callback
class is used for the Call#enqueue(Callback)
method.
只需将 sForecastClient.getWeather(纬度,经度,回调)
更改为 sForecastClient.getWeather(纬度,经度).enqueue(回调) )
并删除第三个参数。
Simply change sForecastClient.getWeather(latitude, longitude, callback)
to sForecastClient.getWeather(latitude, longitude).enqueue(callback)
and remove the third parameter.
这篇关于java.lang中。 RuntimeException未找到Retrofit注释。 (参数#3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!