java 注解 runtime,java.lang中。 RuntimeException未找到Retrofit注释。 (参数#3)

I'm trying to update this RetroFit + Otto tutorial, so my code updated is:

IWeather.java

RetroFit 2.+ doesn't allow to return void, so instead of void getWeather(...) I added Call getWeather(...).

public interface IWeather {

@GET("/{latitude},{longitude}")

Call getWeather(@Path("latitude") String latitude,

@Path("longitude") String longitude,

Callback callback);

}

ForecastClient.java

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 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 callback = new Callback() {

@Override

public void onResponse(Call call, Response response) {

Log.d(ForecastManager.class.getSimpleName(), response.body().toString());

mBus.post(new SendWeatherEvent(response.body()));

}

@Override

public void onFailure(Call call, Throwable t) {

Log.e(ForecastManager.class.getSimpleName(), t.getMessage());

}

};

sForecastClient.getWeather(latitude, longitude, callback);

}

}

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.

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: No Retrofit annotation found. (parameter #3)

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.

Simply change sForecastClient.getWeather(latitude, longitude, callback) to sForecastClient.getWeather(latitude, longitude).enqueue(callback) and remove the third parameter.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值